java线程锁synchronized
import java.util.HashMap;
import java.util.Hashtable;
public class ThreadSynchronized {
//全局变量,多线程同时操作,会出现脏读,写问题
private static int a=0;
private static void threadTest() {
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
synchronized ("") {
a++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("plus:" + Thread.currentThread().getName() + ": " + a);
}
}
}.start();
}
}
public static void main(String[] args) {
threadTest();
}
}```
作者:我是刘先生
地址:https://www.cnblogs.com/cekaigongchengshi/
文章转载请标明出处,如果,您认为阅读这篇博客让您有些收获,不妨点击一下推荐按钮,据说喜欢分享的,后来都成了大神
欢迎扫码关注微信公众号 |