线程安全 synchronize关键字

2/10/2017来源:ASP.NET技巧人气:830

1,同步块         synchronized(监视器){ 方法块 }

任何对象可以充当监视器。但前提得是同一个对象。

public class SaleTicket extends Thread{
	PRivate static int ticket=100;
//	private static Object obj = new Object();
	@Override
	public void run(){
		while(true){
				try {
					Thread.currentThread().sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				show();
			}
		}
	public void show(){
		synchronized(this){
			if(ticket>0){
				System.out.println(ticket--);
			}
		}
	}
	public static void main(String[] args){
		SaleTicket s1 = new SaleTicket();
		s1.start();
		SaleTicket s2 = new SaleTicket();
		s2.start();
		SaleTicket s3 = new SaleTicket();
		s3.start();
		
	}
	
}上面代码,this充当监视器,由于三个Thread对象,所以三个this不一样,起不到同步的效果。如果将this换成静态Object,那么同步起作用。

public class SaleTicket2 implements Runnable {
	private int ticket =100;
	private Object obj = new Object();
	@Override
	public void run() {
		while(true){
			
//			synchronized (this) {
				try {
					Thread.currentThread().sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				show();
//			}
		}
	}
	public void show(){
		synchronized(obj){
			if (ticket > 0) {
				System.out.println(ticket--);
			}
		}
		
	}
	public static void main(String[] main){
		Runnable t = new SaleTicket2();
		Thread t1 = new Thread(t);
		Thread t2 = new Thread(t);
		Thread t3 = new Thread(t);
		t1.start();
		t2.start();
		t3.start();
	}
}此代码中,只是有一个Runnable实例,所以ticket和obj都是共享的,synchronized(this)和synchronize(obj)都能够起到同步效果。因为是线程共享对象。


2、同步方法  


public synchronize 返回值 方法名(){


}


同步方法默认监视器是当前对象this,

public class SaleTicket extends Thread{
	private static int ticket=100;
	private static Object obj = new Object();
	@Override
	public void run(){
		while(true){
				try {
					Thread.currentThread().sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				show();
			}
		}
	public synchronized void show(){
//		synchronized(obj){
			if(ticket>0){
				System.out.println(ticket--);
//			}
		}
	}
	public static void main(String[] args){
		SaleTicket s1 = new SaleTicket();
		s1.start();
		SaleTicket s2 = new SaleTicket();
		s2.start();
		SaleTicket s3 = new SaleTicket();
		s3.start();
		
	}
	
}
上面代码中,因为new了3个对象,this不同,同步不起作用。这就体现了单例模式的重要性。


下面代码同步起作用,因为只有一个Runnable对象,this相同。

public class SaleTicket2 implements Runnable {
	private int ticket =100;
	private Object obj = new Object();
	@Override
	public void run() {
		while(true){
			
//			synchronized (this) {
				try {
					Thread.currentThread().sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				show();
//			}
		}
	}
	public synchronized void show(){
//		synchronized(obj){
			if (ticket > 0) {
				System.out.println(ticket--);
			}
//		}
		
	}
	public static void main(String[] main){
		Runnable t = new SaleTicket2();
		Thread t1 = new Thread(t);
		Thread t2 = new Thread(t);
		Thread t3 = new Thread(t);
		t1.start();
		t2.start();
		t3.start();
	}
}