全站最帅😎
发布于 2020-04-20 / 1298 阅读
0
0

并发编程:synchronized的应用

image.png

synchronized锁的对象包括:

  • this
  • 临界资源对象
  • Class 类对象。

synchronized 除了保障原子性外,其实也保障了可见性。因为 synchronized 无论是同步的方法还是同步的代码块,都会先把主内存的数据拷贝到工作内存中,同步代码块结束,会把工作内存中的数据更新到主内存中,这样主内存中的数据一定是最新的。更重要的是禁用了乱序重组以及保证了值对存储器的写入,这样就可以保证可见性。

synchronized 同步的缺点

  • synchronized关键字可能造成死锁。
  • synchronized关键字同步的时候,不保证公平性,因此会有线程插队的现象。

下面代码演示了同步对象、同步方法、同步代码块

public class SynTest {

	private long count =0;
	private Object obj = new Object();//作为一个锁

	public long getCount() {
		return count;
	}

	public void setCount(long count) {
		this.count = count;
	}

	/*用在同步块上*/
	public void incCount(){
		synchronized (obj){
			count++;
		}
	}

	/*用在方法上*/
	public synchronized void incCount2(){
			count++;
	}

	/*用在同步块上,但是锁的是当前类的对象实例*/
	public void incCount3(){
		synchronized (this){
			count++;
		}
	}

	//线程
	private static class Count extends Thread{

		private SynTest simplOper;

		public Count(SynTest simplOper) {
			this.simplOper = simplOper;
		}

		@Override
		public void run() {
			for(int i=0;i<10000;i++){
				simplOper.incCount();//count = count+10000
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		SynTest simplOper = new SynTest();
		//启动两个线程
		Count count1 = new Count(simplOper);
		Count count2 = new Count(simplOper);
		count1.start();
		count2.start();
		Thread.sleep(50);
		System.out.println(simplOper.count);//20000
	}
}

评论