zoukankan      html  css  js  c++  java
  • synchronized(this)、synchronized(class)与synchronized(Object)的区别

    在多线程开发中,我们经常看到synchronized(this)、synchronized(*.class)与synchronized(任意对象)这几种类型同步方法。但是是否知道这几种写法有什么区别了?下面根据代码来分析:

    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)使用的对象监视器是一个。

    验证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()加上同步。

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

    运行结果:

    结论:

    上面三个小例子我们可以知道,多个线程调用同一个对象中的不同名称的synchronized同步方法或synchronized(this)同步代码块时,是同步的。

    1、synchronized同步方法

    ①对其它的synchronized同步方法或synchronized(this)同步代码块调用是堵塞状态;

    ②同一时间只有一个线程执行synchronized同步方法中的代码。

    2、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锁对类的所有对象实例起作用。
  • 相关阅读:
    CSS选择器
    结构体
    指针的话题
    安卓开源项目周报0208
    前端开源项目周报0207
    iOS开源项目周报0119
    安卓开源项目周报0117
    前端开源项目周报0116
    微信小程序开源项目库汇总
    iOS开源项目周报0112
  • 原文地址:https://www.cnblogs.com/firstdream/p/8334154.html
Copyright © 2011-2022 走看看