synchronized(this)、synchronized(class)與synchronized(Object)的區別

在多線程開發中,咱們常常看到synchronized(this)、synchronized(*.class)與synchronized(任意對象)這幾種類型同步方法。可是是否知道這幾種寫法有什麼區別了?下面根據代碼來分析:java

 

synchronized代碼塊間的同步性

 

  1. package com.zwz.thread.demo1;  
  2.   
  3. public class ObjectService {  
  4.     public void serviceMethodA(){  
  5.         try {  
  6.             synchronized (this) {  
  7.                 System.out.println("A begin time="+System.currentTimeMillis());  
  8.                 Thread.sleep(2000);  
  9.                 System.out.println("A end   time="+System.currentTimeMillis());  
  10.             }  
  11.         } catch (InterruptedException e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.     }  
  15.     public void serviceMethodB(){  
  16.         synchronized (this) {  
  17.             System.out.println("B begin time="+System.currentTimeMillis());  
  18.             System.out.println("B end   time="+System.currentTimeMillis());  
  19.         }  
  20.     }  
  21. }  
package com.zwz.thread.demo1;

public class ObjectService {
	public void serviceMethodA(){
		try {
			synchronized (this) {
				System.out.println("A begin time="+System.currentTimeMillis());
				Thread.sleep(2000);
				System.out.println("A end   time="+System.currentTimeMillis());
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public void serviceMethodB(){
		synchronized (this) {
			System.out.println("B begin time="+System.currentTimeMillis());
			System.out.println("B end   time="+System.currentTimeMillis());
		}
	}
}
  1. package com.zwz.thread.demo1;  
  2.   
  3. public class ThreadA extends Thread {  
  4.     private ObjectService objectService;  
  5.     public ThreadA(ObjectService objectService){  
  6.         super();  
  7.         this.objectService=objectService;  
  8.     }  
  9.     @Override  
  10.     public void run() {  
  11.         super.run();  
  12.         objectService.serviceMethodA();  
  13.     }  
  14. }  
package com.zwz.thread.demo1;

public class ThreadA extends Thread {
	private ObjectService objectService;
	public ThreadA(ObjectService objectService){
		super();
		this.objectService=objectService;
	}
	@Override
	public void run() {
		super.run();
		objectService.serviceMethodA();
	}
}
  1. package com.zwz.thread.demo1;  
  2.   
  3. public class ThreadB extends Thread {  
  4.     private ObjectService objectService;  
  5.     public ThreadB(ObjectService objectService){  
  6.         super();  
  7.         this.objectService=objectService;  
  8.     }  
  9.     @Override  
  10.     public void run() {  
  11.         super.run();  
  12.         objectService.serviceMethodB();  
  13.     }  
  14. }  
package com.zwz.thread.demo1;

public class ThreadB extends Thread {
	private ObjectService objectService;
	public ThreadB(ObjectService objectService){
		super();
		this.objectService=objectService;
	}
	@Override
	public void run() {
		super.run();
		objectService.serviceMethodB();
	}
}

 

 

  1. package com.zwz.thread.demo1;  
  2.   
  3. public class MainTest {  
  4.     public static void main(String[] args) {  
  5.         ObjectService service=new ObjectService();  
  6.         ThreadA a=new ThreadA(service);  
  7.         a.setName("a");  
  8.         a.start();  
  9.         ThreadB b=new ThreadB(service);  
  10.         b.setName("b");  
  11.         b.start();  
  12.     }  
  13. }  
package com.zwz.thread.demo1;

public class MainTest {
	public static void main(String[] args) {
		ObjectService service=new ObjectService();
		ThreadA a=new ThreadA(service);
		a.setName("a");
		a.start();
		ThreadB b=new ThreadB(service);
		b.setName("b");
		b.start();
	}
}

運行結果:多線程

結論:異步

當一個線程訪問ObjectService的一個synchronized (this)同步代碼塊時,其它線程對同一個ObjectService中其它的synchronized (this)同步代碼塊的訪問將是堵塞,這說明synchronized (this)使用的對象監視器是一個。ide

 

驗證synchronized (this)代碼塊是鎖定當前對象

 

  1. package com.zwz.thread.demo2;  
  2.   
  3. public class ObjectService {  
  4.     public void objectMethodA(){  
  5.         System.out.println("run----objectMethodA");  
  6.     }  
  7.     public void objectMethodB(){  
  8.         synchronized (this) {  
  9.             try {  
  10.                 for (int i = 1; i <= 10; i++) {  
  11.                     System.out.println("synchronized thread name:"+Thread.currentThread().getName()+"-->i="+i);  
  12.                     Thread.sleep(1000);  
  13.                 }  
  14.             } catch (InterruptedException e) {  
  15.                     e.printStackTrace();  
  16.             }  
  17.         }  
  18.     }  
  19. }  
package com.zwz.thread.demo2;

public class ObjectService {
	public void objectMethodA(){
		System.out.println("run----objectMethodA");
	}
	public void objectMethodB(){
		synchronized (this) {
			try {
				for (int i = 1; i <= 10; i++) {
					System.out.println("synchronized thread name:"+Thread.currentThread().getName()+"-->i="+i);
					Thread.sleep(1000);
				}
			} catch (InterruptedException e) {
					e.printStackTrace();
			}
		}
	}
}
  1. package com.zwz.thread.demo2;  
  2.   
  3. public class ThreadA extends Thread {  
  4.     private ObjectService objectService;  
  5.   
  6.     public ThreadA(ObjectService objectService) {  
  7.         super();  
  8.         this.objectService = objectService;  
  9.     }  
  10.     @Override  
  11.     public void run() {  
  12.         super.run();  
  13.         objectService.objectMethodA();  
  14.     }  
  15. }  
package com.zwz.thread.demo2;

public class ThreadA extends Thread {
	private ObjectService objectService;

	public ThreadA(ObjectService objectService) {
		super();
		this.objectService = objectService;
	}
	@Override
	public void run() {
		super.run();
		objectService.objectMethodA();
	}
}
  1. package com.zwz.thread.demo2;  
  2.   
  3. public class ThreadB extends Thread {  
  4.     private ObjectService objectService;  
  5.   
  6.     public ThreadB(ObjectService objectService) {  
  7.         super();  
  8.         this.objectService = objectService;  
  9.     }  
  10.     @Override  
  11.     public void run() {  
  12.         super.run();  
  13.         objectService.objectMethodB();  
  14.     }  
  15. }  
package com.zwz.thread.demo2;

public class ThreadB extends Thread {
	private ObjectService objectService;

	public ThreadB(ObjectService objectService) {
		super();
		this.objectService = objectService;
	}
	@Override
	public void run() {
		super.run();
		objectService.objectMethodB();
	}
}
  1. package com.zwz.thread.demo2;  
  2.   
  3. public class MainTest {  
  4.     public static void main(String[] args) throws InterruptedException {  
  5.         ObjectService service=new ObjectService();  
  6.         ThreadB b=new ThreadB(service);  
  7.         b.start();  
  8.         Thread.sleep(2000);  
  9.         ThreadA a=new ThreadA(service);  
  10.         a.start();  
  11.     }  
  12. }  
package com.zwz.thread.demo2;

public class MainTest {
	public static void main(String[] args) throws InterruptedException {
		ObjectService service=new ObjectService();
		ThreadB b=new ThreadB(service);
		b.start();
		Thread.sleep(2000);
		ThreadA a=new ThreadA(service);
		a.start();
	}
}

運行結果:測試

 

能夠看到objectMethodA方法異步執行了,下面咱們將objectMethodA()加上同步。this

 

  1. package com.zwz.thread.demo2;  
  2.   
  3. public class ObjectService {  
  4.     public synchronized void objectMethodA(){  
  5.         System.out.println("run----objectMethodA");  
  6.     }  
  7.     public void objectMethodB(){  
  8.         synchronized (this) {  
  9.             try {  
  10.                 for (int i = 1; i <= 10; i++) {  
  11.                     System.out.println("synchronized thread name:"+Thread.currentThread().getName()+"-->i="+i);  
  12.                     Thread.sleep(1000);  
  13.                 }  
  14.             } catch (InterruptedException e) {  
  15.                     e.printStackTrace();  
  16.             }  
  17.         }  
  18.     }  
  19. }  
package com.zwz.thread.demo2;

public class ObjectService {
	public synchronized void objectMethodA(){
		System.out.println("run----objectMethodA");
	}
	public void objectMethodB(){
		synchronized (this) {
			try {
				for (int i = 1; i <= 10; i++) {
					System.out.println("synchronized thread name:"+Thread.currentThread().getName()+"-->i="+i);
					Thread.sleep(1000);
				}
			} catch (InterruptedException e) {
					e.printStackTrace();
			}
		}
	}
}

運行結果:spa

 

結論:.net

上面三個小例子咱們能夠知道,多個線程調用同一個對象中的不一樣名稱的synchronized同步方法或synchronized(this)同步代碼塊時,是同步的。線程

一、synchronized同步方法code

①對其它的synchronized同步方法或synchronized(this)同步代碼塊調用是堵塞狀態;

②同一時間只有一個線程執行synchronized同步方法中的代碼。

二、synchronized(this)同步代碼塊

 

①對其它的synchronized同步方法或synchronized(this)同步代碼塊調用是堵塞狀態;

②同一時間只有一個線程執行synchronized同步方法中的代碼。

將任意對象做爲對象監視器

 

  1. package com.zwz.thread.demo3;  
  2.   
  3. public class ObjectService {  
  4.     private String uname;  
  5.     private String pwd;  
  6.     String lock=new String();  
  7.     public void setUserNamePassWord(String userName,String passWord){  
  8.         try {  
  9.             synchronized (lock) {  
  10.                 System.out.println("thread name="+Thread.currentThread().getName()  
  11.                         +" 進入代碼快:"+System.currentTimeMillis());  
  12.                 uname=userName;  
  13.                 Thread.sleep(3000);  
  14.                 pwd=passWord;  
  15.                 System.out.println("thread name="+Thread.currentThread().getName()  
  16.                         +" 進入代碼快:"+System.currentTimeMillis()+"入參uname:"+uname+"入參pwd:"+pwd);  
  17.             }  
  18.         } catch (InterruptedException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22. }  
package com.zwz.thread.demo3;

public class ObjectService {
	private String uname;
	private String pwd;
	String lock=new String();
	public void setUserNamePassWord(String userName,String passWord){
		try {
			synchronized (lock) {
				System.out.println("thread name="+Thread.currentThread().getName()
						+" 進入代碼快:"+System.currentTimeMillis());
				uname=userName;
				Thread.sleep(3000);
				pwd=passWord;
				System.out.println("thread name="+Thread.currentThread().getName()
						+" 進入代碼快:"+System.currentTimeMillis()+"入參uname:"+uname+"入參pwd:"+pwd);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
  1. package com.zwz.thread.demo3;  
  2.   
  3. public class ThreadA extends Thread {  
  4.     private ObjectService objectService;  
  5.   
  6.     public ThreadA(ObjectService objectService) {  
  7.         super();  
  8.         this.objectService = objectService;  
  9.     }  
  10.     @Override  
  11.     public void run() {  
  12.         objectService.setUserNamePassWord("a", "aa");  
  13.     }  
  14. }  
package com.zwz.thread.demo3;

public class ThreadA extends Thread {
	private ObjectService objectService;

	public ThreadA(ObjectService objectService) {
		super();
		this.objectService = objectService;
	}
	@Override
	public void run() {
		objectService.setUserNamePassWord("a", "aa");
	}
}
  1. package com.zwz.thread.demo3;  
  2.   
  3. public class ThreadB extends Thread {  
  4.     private ObjectService objectService;  
  5.   
  6.     public ThreadB(ObjectService objectService) {  
  7.         super();  
  8.         this.objectService = objectService;  
  9.     }  
  10.     @Override  
  11.     public void run() {  
  12.         objectService.setUserNamePassWord("b", "bb");  
  13.     }  
  14. }  
package com.zwz.thread.demo3;

public class ThreadB extends Thread {
	private ObjectService objectService;

	public ThreadB(ObjectService objectService) {
		super();
		this.objectService = objectService;
	}
	@Override
	public void run() {
		objectService.setUserNamePassWord("b", "bb");
	}
}
  1. package com.zwz.thread.demo3;  
  2.   
  3. public class MainTest {  
  4.     public static void main(String[] args) {  
  5.         ObjectService service=new ObjectService();  
  6.         ThreadA a=new ThreadA(service);  
  7.         a.setName("A");  
  8.         a.start();  
  9.         ThreadB b=new ThreadB(service);  
  10.         b.setName("B");  
  11.         b.start();  
  12.     }  
  13. }  
package com.zwz.thread.demo3;

public class MainTest {
	public static void main(String[] args) {
		ObjectService service=new ObjectService();
		ThreadA a=new ThreadA(service);
		a.setName("A");
		a.start();
		ThreadB b=new ThreadB(service);
		b.setName("B");
		b.start();
	}
}

運行結果:

 

下面我把String lock=new String();放在方法中會有啥結果了:

 

  1. package com.zwz.thread.demo3;  
  2.   
  3. public class ObjectService {  
  4.     private String uname;  
  5.     private String pwd;  
  6.     public void setUserNamePassWord(String userName,String passWord){  
  7.         try {  
  8.             String lock=new String();  
  9.             synchronized (lock) {  
  10.                 System.out.println("thread name="+Thread.currentThread().getName()  
  11.                         +" 進入代碼快:"+System.currentTimeMillis());  
  12.                 uname=userName;  
  13.                 Thread.sleep(3000);  
  14.                 pwd=passWord;  
  15.                 System.out.println("thread name="+Thread.currentThread().getName()  
  16.                         +" 進入代碼快:"+System.currentTimeMillis()+"入參uname:"+uname+"入參pwd:"+pwd);  
  17.             }  
  18.         } catch (InterruptedException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22. }  
package com.zwz.thread.demo3;

public class ObjectService {
	private String uname;
	private String pwd;
	public void setUserNamePassWord(String userName,String passWord){
		try {
			String lock=new String();
			synchronized (lock) {
				System.out.println("thread name="+Thread.currentThread().getName()
						+" 進入代碼快:"+System.currentTimeMillis());
				uname=userName;
				Thread.sleep(3000);
				pwd=passWord;
				System.out.println("thread name="+Thread.currentThread().getName()
						+" 進入代碼快:"+System.currentTimeMillis()+"入參uname:"+uname+"入參pwd:"+pwd);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

運行結果:

 

 

結論:

多個線程持有對象監視器做爲同一個對象的前提下,同一時間只有一個線程能夠執行synchronized(任意自定義對象)同步代碼快。

 

synchronized(任意自定義對象)與synchronized同步方法共用

 

  1. package com.zwz.thread.demo4;  
  2.   
  3. public class ObjectService {  
  4.     private String lock=new String();  
  5.     public void methodA(){  
  6.         try {  
  7.             synchronized (lock) {  
  8.                 System.out.println("a begin");  
  9.                 Thread.sleep(3000);  
  10.                 System.out.println("a   end");  
  11.             }  
  12.         } catch (InterruptedException e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.     }  
  16.     public synchronized void methodB(){  
  17.         System.out.println("b begin");  
  18.         System.out.println("b   end");  
  19.     }  
  20. }  
package com.zwz.thread.demo4;

public class ObjectService {
	private String lock=new String();
	public void methodA(){
		try {
			synchronized (lock) {
				System.out.println("a begin");
				Thread.sleep(3000);
				System.out.println("a   end");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public synchronized void methodB(){
		System.out.println("b begin");
		System.out.println("b   end");
	}
}
  1. package com.zwz.thread.demo4;  
  2.   
  3. public class ThreadA extends Thread {  
  4.     private ObjectService objectService;  
  5.   
  6.     public ThreadA(ObjectService objectService) {  
  7.         super();  
  8.         this.objectService = objectService;  
  9.     }  
  10.     @Override  
  11.     public void run() {  
  12.         objectService.methodA();  
  13.     }  
  14. }  
package com.zwz.thread.demo4;

public class ThreadA extends Thread {
	private ObjectService objectService;

	public ThreadA(ObjectService objectService) {
		super();
		this.objectService = objectService;
	}
	@Override
	public void run() {
		objectService.methodA();
	}
}
  1. package com.zwz.thread.demo4;  
  2.   
  3. public class ThreadB extends Thread {  
  4.     private ObjectService objectService;  
  5.   
  6.     public ThreadB(ObjectService objectService) {  
  7.         super();  
  8.         this.objectService = objectService;  
  9.     }  
  10.     @Override  
  11.     public void run() {  
  12.         objectService.methodB();  
  13.     }  
  14. }  
package com.zwz.thread.demo4;

public class ThreadB extends Thread {
	private ObjectService objectService;

	public ThreadB(ObjectService objectService) {
		super();
		this.objectService = objectService;
	}
	@Override
	public void run() {
		objectService.methodB();
	}
}
  1. package com.zwz.thread.demo4;  
  2.   
  3. public class MainTest {  
  4.     public static void main(String[] args) {  
  5.         ObjectService service=new ObjectService();  
  6.         ThreadA a=new ThreadA(service);  
  7.         a.setName("A");  
  8.         a.start();  
  9.         ThreadB b=new ThreadB(service);  
  10.         b.setName("B");  
  11.         b.start();  
  12.     }  
  13.       
  14. }  
package com.zwz.thread.demo4;

public class MainTest {
	public static void main(String[] args) {
		ObjectService service=new ObjectService();
		ThreadA a=new ThreadA(service);
		a.setName("A");
		a.start();
		ThreadB b=new ThreadB(service);
		b.setName("B");
		b.start();
	}
	
}

運行結果:

 

結論:

使用synchronized(任意自定義對象)進行同步操做,對象監視器必須是同一個對象。不過不是同一個,運行就是異步執行了。

 

靜態同步synchronized方法與synchronized(*.class)代碼塊

 

靜態同步synchronized方法

  1. package com.zwz.thread.demo6;  
  2.   
  3. public class ObjectService {  
  4.     public synchronized static void methodA(){  
  5.         try {  
  6.             System.out.println("static methodA begin 線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());  
  7.             Thread.sleep(3000);  
  8.             System.out.println("static methodA end   線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());  
  9.         } catch (InterruptedException e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.     }  
  13.       
  14.     public synchronized static void methodB(){  
  15.         System.out.println("static methodB begin 線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());  
  16.         System.out.println("static methodB end   線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());  
  17.     }  
  18. }  
package com.zwz.thread.demo6;

public class ObjectService {
	public synchronized static void methodA(){
		try {
			System.out.println("static methodA begin 線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
			Thread.sleep(3000);
			System.out.println("static methodA end   線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public synchronized static void methodB(){
		System.out.println("static methodB begin 線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
		System.out.println("static methodB end   線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
	}
}
  1. package com.zwz.thread.demo6;  
  2.   
  3. public class ThreadA extends Thread {  
  4.   
  5.     @Override  
  6.     public void run() {  
  7.         ObjectService.methodA();  
  8.     }  
  9. }  
package com.zwz.thread.demo6;

public class ThreadA extends Thread {

	@Override
	public void run() {
		ObjectService.methodA();
	}
}
  1. package com.zwz.thread.demo6;  
  2.   
  3. public class ThreadB extends Thread {  
  4.     @Override  
  5.     public void run() {  
  6.         ObjectService.methodB();  
  7.     }  
  8. }  
package com.zwz.thread.demo6;

public class ThreadB extends Thread {
	@Override
	public void run() {
		ObjectService.methodB();
	}
}
  1. package com.zwz.thread.demo6;  
  2.   
  3. public class MainTest {  
  4.     public static void main(String[] args) {  
  5.         ThreadA a=new ThreadA();  
  6.         a.setName("A");  
  7.         a.start();  
  8.         ThreadB b=new ThreadB();  
  9.         b.setName("B");  
  10.         b.start();  
  11.     }  
  12. }  
package com.zwz.thread.demo6;

public class MainTest {
	public static void main(String[] args) {
		ThreadA a=new ThreadA();
		a.setName("A");
		a.start();
		ThreadB b=new ThreadB();
		b.setName("B");
		b.start();
	}
}
運行結果:
結論:
synchronized應用在static方法上,那是對當前對應的*.Class進行持鎖。
 

synchronized(*.class)代碼塊

  1. package com.zwz.thread.demo7;  
  2.   
  3. public class ObjectService {  
  4.     public void methodA(){  
  5.         try {  
  6.             synchronized (ObjectService.class) {  
  7.                 System.out.println("methodA begin 線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());  
  8.                 Thread.sleep(3000);  
  9.                 System.out.println("methodA end   線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());  
  10.             }  
  11.         } catch (InterruptedException e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.     }  
  15.       
  16.     public void methodB(){  
  17.         synchronized (ObjectService.class) {  
  18.             System.out.println("methodB begin 線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());  
  19.             System.out.println("methodB end   線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());  
  20.         }  
  21.     }  
  22. }  
package com.zwz.thread.demo7;

public class ObjectService {
	public void methodA(){
		try {
			synchronized (ObjectService.class) {
				System.out.println("methodA begin 線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
				Thread.sleep(3000);
				System.out.println("methodA end   線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public void methodB(){
		synchronized (ObjectService.class) {
			System.out.println("methodB begin 線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
			System.out.println("methodB end   線程名稱:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
		}
	}
}
  1. package com.zwz.thread.demo7;  
  2.   
  3. public class ThreadA extends Thread {  
  4.     private ObjectService objectService;  
  5.   
  6.     public ThreadA(ObjectService objectService) {  
  7.         super();  
  8.         this.objectService = objectService;  
  9.     }  
  10.     @Override  
  11.     public void run() {  
  12.         objectService.methodA();  
  13.     }  
  14. }  
package com.zwz.thread.demo7;

public class ThreadA extends Thread {
	private ObjectService objectService;

	public ThreadA(ObjectService objectService) {
		super();
		this.objectService = objectService;
	}
	@Override
	public void run() {
		objectService.methodA();
	}
}
  1. package com.zwz.thread.demo7;  
  2.   
  3. public class ThreadB extends Thread {  
  4.     private ObjectService objectService;  
  5.   
  6.     public ThreadB(ObjectService objectService) {  
  7.         super();  
  8.         this.objectService = objectService;  
  9.     }  
  10.     @Override  
  11.     public void run() {  
  12.         objectService.methodB();  
  13.     }  
  14. }  
package com.zwz.thread.demo7;

public class ThreadB extends Thread {
	private ObjectService objectService;

	public ThreadB(ObjectService objectService) {
		super();
		this.objectService = objectService;
	}
	@Override
	public void run() {
		objectService.methodB();
	}
}
  1. package com.zwz.thread.demo7;  
  2.   
  3. public class MainTest {  
  4.     public static void main(String[] args) {  
  5.         ObjectService service=new ObjectService();  
  6.         ThreadA a=new ThreadA(service);  
  7.         a.setName("A");  
  8.         a.start();  
  9.         ThreadB b=new ThreadB(service);  
  10.         b.setName("B");  
  11.         b.start();  
  12.     }  
  13. }  
package com.zwz.thread.demo7;

public class MainTest {
	public static void main(String[] args) {
		ObjectService service=new ObjectService();
		ThreadA a=new ThreadA(service);
		a.setName("A");
		a.start();
		ThreadB b=new ThreadB(service);
		b.setName("B");
		b.start();
	}
}
運行結果:
上面測試方法是共同對象,下面咱們分別實例化一個對象:
  1. package com.zwz.thread.demo7;  
  2.   
  3. public class MainTest {  
  4.     public static void main(String[] args) {  
  5.         ObjectService service1=new ObjectService();  
  6.         ObjectService service2=new ObjectService();  
  7.         ThreadA a=new ThreadA(service1);  
  8.         a.setName("A");  
  9.         a.start();  
  10.         ThreadB b=new ThreadB(service2);  
  11.         b.setName("B");  
  12.         b.start();  
  13.     }  
  14. }  
package com.zwz.thread.demo7;

public class MainTest {
	public static void main(String[] args) {
		ObjectService service1=new ObjectService();
		ObjectService service2=new ObjectService();
		ThreadA a=new ThreadA(service1);
		a.setName("A");
		a.start();
		ThreadB b=new ThreadB(service2);
		b.setName("B");
		b.start();
	}
}
運行結果:
結論:
同步synchronized(*.class)代碼塊的做用其實和synchronized static方法做用同樣。Class鎖對類的全部對象實例起做用。
相關文章
相關標籤/搜索