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();
	
	}
	

}```
posted @ 2020-12-08 14:46  测开工程师成长之路  阅读(120)  评论(0编辑  收藏  举报