验证同步代码块与同步函数的锁【多线程】

转载地址:http://blog.csdn.net/qq_24653023/article/details/51763731




通过一个卖票问题,验证多线程中同步代码块与同步函数的锁。


卖票系统,两台机器t1线程和t2线程,共同卖掉100张票。


[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package day13;  
  2. public class SynFunctionLockDemo {  
  3.     public static void main (String[] args){  
  4.         Ticket t =new Ticket();//一个对象,共享100张票  
  5.         System.out.println("t   :"+t);  
  6.         Thread t1 = new Thread(t);  
  7.         Thread t2 = new Thread(t);  
  8.         t1.start();  
  9.         try{Thread.sleep(10);}catch(InterruptedException e){}  
  10.           
  11.         t.flag = false;  
  12.         t2.start();  
  13.         /*解释第九行 这一步, 
  14.         *在主进程main中,t1.start和t2.start以及t.flag=false同时运行到了 
  15.         *主线程进行了flag->false的过程 
  16.         *t1和t2线程都只能在flag=false的同步函数中进行 
  17.         */  
  18.     }  
  19. }  
  20. class Ticket implements Runnable{  
  21.     private int num = 100;  
  22.     boolean flag =true;  
  23.     Object obj = new Object();  
  24.     public void run(){  
  25.         System.out.println("this:"+this);  
  26.         if(flag){//同步代码块  
  27.             for(int i=200;i>1;i--){  
  28.                 synchronized(obj){  
  29.                     if(num>0){  
  30.                         try{Thread.sleep(10);}catch(InterruptedException e){}  
  31.                         System.out.println(Thread.currentThread().getName()+"...obj..."+num--);  
  32.                     }  
  33.                 }  
  34.             }  
  35.         }  
  36.         else{  
  37.             for(int i=200;i>1;i--)  
  38.                 show();  
  39.             }  
  40.     }  
  41.     public synchronized void show(){//同步函数  
  42.         if(num>0){  
  43.             try{Thread.sleep(10);}catch(InterruptedException e){}  
  44.             System.out.println(Thread.currentThread().getName()+"...function.."+num--);  
  45.         }  
  46.     }  
  47. }  



运行之后,结果会出现第0号票的可能,出现错误,说明同步函数和同步代码块的锁是不相同的。


如果将28行的synchronized(obj)改成synchronized(this)  



结果不会出现第0号票,正确,这种改法保证了同步代码块和同步函数锁的一致性,同时也验证同步代码块与同步函数的锁是不一样的。

 

同步函数的使用的锁是this

同步函数和同步代码块的区别:

同步函数的锁是固定的this

同步代码块的锁是任意的对象。

建议使用同步代码块。


通过一个卖票问题,验证多线程中同步代码块与同步函数的锁。


卖票系统,两台机器t1线程和t2线程,共同卖掉100张票。


[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package day13;  
  2. public class SynFunctionLockDemo {  
  3.     public static void main (String[] args){  
  4.         Ticket t =new Ticket();//一个对象,共享100张票  
  5.         System.out.println("t   :"+t);  
  6.         Thread t1 = new Thread(t);  
  7.         Thread t2 = new Thread(t);  
  8.         t1.start();  
  9.         try{Thread.sleep(10);}catch(InterruptedException e){}  
  10.           
  11.         t.flag = false;  
  12.         t2.start();  
  13.         /*解释第九行 这一步, 
  14.         *在主进程main中,t1.start和t2.start以及t.flag=false同时运行到了 
  15.         *主线程进行了flag->false的过程 
  16.         *t1和t2线程都只能在flag=false的同步函数中进行 
  17.         */  
  18.     }  
  19. }  
  20. class Ticket implements Runnable{  
  21.     private int num = 100;  
  22.     boolean flag =true;  
  23.     Object obj = new Object();  
  24.     public void run(){  
  25.         System.out.println("this:"+this);  
  26.         if(flag){//同步代码块  
  27.             for(int i=200;i>1;i--){  
  28.                 synchronized(obj){  
  29.                     if(num>0){  
  30.                         try{Thread.sleep(10);}catch(InterruptedException e){}  
  31.                         System.out.println(Thread.currentThread().getName()+"...obj..."+num--);  
  32.                     }  
  33.                 }  
  34.             }  
  35.         }  
  36.         else{  
  37.             for(int i=200;i>1;i--)  
  38.                 show();  
  39.             }  
  40.     }  
  41.     public synchronized void show(){//同步函数  
  42.         if(num>0){  
  43.             try{Thread.sleep(10);}catch(InterruptedException e){}  
  44.             System.out.println(Thread.currentThread().getName()+"...function.."+num--);  
  45.         }  
  46.     }  
  47. }  



运行之后,结果会出现第0号票的可能,出现错误,说明同步函数和同步代码块的锁是不相同的。


如果将28行的synchronized(obj)改成synchronized(this)  



结果不会出现第0号票,正确,这种改法保证了同步代码块和同步函数锁的一致性,同时也验证同步代码块与同步函数的锁是不一样的。

 

同步函数的使用的锁是this

同步函数和同步代码块的区别:

同步函数的锁是固定的this

同步代码块的锁是任意的对象。

建议使用同步代码块。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值