同步方法与同步块的区别

java使用synchronized同步,分为四种情况:

  实例方法同步

  实例方法中同步块

  静态方法同步

  静态方法中同步块


我们从两个方面来说他们的不同,一个是同步方法和同步块的区别,一个是静态和非静态的区别。

同步方法就是在方法前加关键字synchronized,然后被同步的方法一次只能有一个线程进入,其他线程等待。而同步方法则是在方法内部使用大括号使得一个代码块得到同步。同步块会有一个同步的”目标“,使得同步块更加灵活一些(同步块可以通过”目标“决定需要锁定的对象)。一般情况下,如果此”目标“为this,那么同步方法和同步块没有太大的区别。

另外,通过反编译可以看出,同步块比同步方法多了两个指令。因此同步方法是比同步块要快一些。



非静态和静态的区别主要在于(以同步方法为例):非静态的同步方法是锁定类的实例的,而静态的同步方法是锁定类的;

也就是说,对于非静态的同步方法,在同一时刻,一个类的一个实例中,只有一个线程能进入同步的方法。但是对于多个实例,每一个实例的一个线程都可以进入同一同步的方法。

Demo1:一个实例的多个线程,一次只能有一个线程进入非静态同步的方法。

[java]  view plain  copy
  1. package SynchronizedTest;  
  2.   
  3. /** 
  4.  * Created by carrot on 16/8/31. 
  5.  */  
  6. public class SyncFunc {  
  7.   
  8.     public synchronized void func1() {  
  9.         System.out.println(Thread.currentThread().getName() + " is running");  
  10.         try {  
  11.             Thread.sleep(3000);  
  12.         } catch (InterruptedException e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.         System.out.println(Thread.currentThread().getName() + " is stop");  
  16.     }  
  17.   
  18.     public static void main(String[] args) {  
  19.   
  20.         NewThread newThread1 = new NewThread();  
  21.         NewThread newThread2 = new NewThread();  
  22.         NewThread newThread3 = new NewThread();  
  23.   
  24.         newThread1.start();  
  25.         newThread2.start();  
  26.         newThread3.start();  
  27.   
  28.     }  
  29. }  
  30.   
  31. class NewThread extends Thread {  
  32.   
  33.     static SyncFunc syncFunc = new SyncFunc();  
  34.   
  35.     @Override  
  36.     public void run() {  
  37.         syncFunc.func1();  
  38.     }  
  39.   
  40. }  

结果:

[java]  view plain  copy
  1. Thread-0 is running  
  2. Thread-0 is stop  
  3. Thread-2 is running  
  4. Thread-2 is stop  
  5. Thread-1 is running  
  6. Thread-1 is stop  

从结果可见,每次只能有一个线程进入非静态同步的方法。


Demo2:多个实例的线程能同时进入非静态同步的方法。

[java]  view plain  copy
  1. package SynchronizedTest;  
  2.   
  3. /** 
  4.  * Created by carrot on 16/8/31. 
  5.  */  
  6. public class SyncFunc {  
  7.   
  8.     public synchronized void func1() {  
  9.         System.out.println(Thread.currentThread().getName() + " is running");  
  10.         try {  
  11.             Thread.sleep(3000);  
  12.         } catch (InterruptedException e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.         System.out.println(Thread.currentThread().getName() + " is stop");  
  16.     }  
  17.   
  18.     public static void main(String[] args) {  
  19.   
  20.         NewThread newThread1 = new NewThread();  
  21.         NewThread newThread2 = new NewThread();  
  22.         NewThread newThread3 = new NewThread();  
  23.   
  24.         newThread1.start();  
  25.         newThread2.start();  
  26.         newThread3.start();  
  27.   
  28.     }  
  29. }  
  30.   
  31. class NewThread extends Thread {  
  32.   
  33.     SyncFunc syncFunc = new SyncFunc();  
  34.   
  35.     @Override  
  36.     public void run() {  
  37.         syncFunc.func1();  
  38.     }  
  39.   
  40. }  

结果:

[java]  view plain  copy
  1. Thread-0 is running  
  2. Thread-1 is running  
  3. Thread-2 is running  
  4. Thread-0 is stop  
  5. Thread-2 is stop  
  6. Thread-1 is stop  

从结果可以看出,多个实例的线程同时进入了同步的非静态方法。


Demo3:多个实例的线程进入静态的同步方法。

[java]  view plain  copy
  1. package SynchronizedTest;  
  2.   
  3. /** 
  4.  * Created by carrot on 16/8/31. 
  5.  */  
  6. public class SyncFunc {  
  7.   
  8.     public static synchronized void func1() {  
  9.         System.out.println(Thread.currentThread().getName() + " is running");  
  10.         try {  
  11.             Thread.sleep(3000);  
  12.         } catch (InterruptedException e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.         System.out.println(Thread.currentThread().getName() + " is stop");  
  16.     }  
  17.   
  18.     public static void main(String[] args) {  
  19.   
  20.         NewThread newThread1 = new NewThread();  
  21.         NewThread newThread2 = new NewThread();  
  22.         NewThread newThread3 = new NewThread();  
  23.   
  24.         newThread1.start();  
  25.         newThread2.start();  
  26.         newThread3.start();  
  27.   
  28.     }  
  29. }  
  30.   
  31. class NewThread extends Thread {  
  32.   
  33.     SyncFunc syncFunc = new SyncFunc();  
  34.   
  35.     @Override  
  36.     public void run() {  
  37.         syncFunc.func1();  
  38.     }  
  39.   
  40. }  

结果:

[java]  view plain  copy
  1. Thread-0 is running  
  2. Thread-0 is stop  
  3. Thread-2 is running  
  4. Thread-2 is stop  
  5. Thread-1 is running  
  6. Thread-1 is stop  

从结果可以看出,对于同一个对象的多个实例,在进入静态的同步方法时,一次只能有一个类实例进入。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值