3个线程依次轮流打印出75个数

转自:详解使用synchronized解决三个线程依次轮流打印出75个数



原帖见:http://www.iteye.com/topic/1117703

问题描述:

一个关于线程的经典面试题,要求用三个线程,按顺序打印1,2,3,4,5.... 71,72,73,74, 75.

线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推, 直到线程3打印到75。

 

直接上代码:

 

Java代码   收藏代码
  1. package concurrent.test;  
  2.   
  3.   
  4. /** 
  5.  * 要求创建三个线程,输出1-75, 
  6.  * 最开始第一个线程输出1-5,第二个输出6-10,第三个输出11-15 
  7.  * 接着再第一个线程输出16-20...就这样循环下去,直到打印出75个数 
  8.  * @author qiaoxueshi 
  9.  * 
  10.  */  
  11. public class Print1to75 {  
  12.     static class Printer implements Runnable{  
  13.         static int num = 1//开始数字  
  14.         static final int END = 75;  
  15.         int id;  
  16.           
  17.         public Printer(int id) {  
  18.             this.id = id;  
  19.         }  
  20.           
  21.         @Override  
  22.         public void run(){  
  23.             synchronized (Printer.class) {  
  24.                 while(num <= END){  
  25.                     if(num / 5 % 3 == id){ //如果是属于自己的数,依次打印出来五个  
  26.                         System.out.print(id + ":");  
  27.                         for(int i = 0; i < 5; i++){  
  28.                             System.out.print(num++ + ", ");  
  29.                         }  
  30.                         System.out.println();  
  31.                           
  32.                         Printer.class.notifyAll();//放弃CPU使用权,唤醒等待在Print.class队列上的的打印线程  
  33.                     }else{  
  34.                         try {  
  35.                             Printer.class.wait();//如果不属于自己的数,把当前线程挂在Printer.class这个对象的等待队列上(也是放弃CPU使用权),等待唤醒  
  36.                         } catch (InterruptedException e) {  
  37.                             System.out.println("id" + "被打断了");  
  38.                         }  
  39.                     }  
  40.                 }  
  41.             }  
  42.         }  
  43.     }  
  44.       
  45.       
  46.     public static void main(String[] args) {  
  47.         //下面可以不按0,1,2的顺序来,而且在两两中间随便sleep(),都会正确打印出来  
  48.         new Thread( new Printer(0)).start();  
  49.         new Thread( new Printer(1)).start();  
  50.         new Thread( new Printer(2)).start();  
  51.     }  
  52. }  

 

注释中说的也很明白,有问题欢迎大家讨论。

 

 

 

结果(运行了N次,结果都是一致的,请大家检验):

 

Java代码   收藏代码
  1. 0:12345,   
  2. 1:678910,   
  3. 2:1112131415,   
  4. 0:1617181920,   
  5. 1:2122232425,   
  6. 2:2627282930,   
  7. 0:3132333435,   
  8. 1:3637383940,   
  9. 2:4142434445,   
  10. 0:4647484950,   
  11. 1:5152535455,   
  12. 2:5657585960,   
  13. 0:6162636465,   
  14. 1:6667686970,   
  15. 2:7172737475,   

 

 

注意第23行的synchronized (Printer.class) ,为什么是Printer.class,而不是this呢?

是因为Print.class也是一个对象,在当前JVM中是唯一的,它相当于一个“公证人”,三个线程竞争资源的时候都是从唯一的这个“公证人”手里拿到许可,才能进入synchronized体。

而如果是synchronized (this)的话,this也相当于一个“公证人”,那么三个线程各自有一个“公证人”,相当于各干各的,三个中间没有竞争关系,构不成同步。

 

可见只要是这三个的“公证人”是同一个家伙,就能保持同步,稍微修改一下代码,我们给三个线程传进去同一个“公证人”(其实就是一个普通的不能再普通的对象):

 

 

Java代码   收藏代码
  1. package concurrent.test;  
  2.   
  3.   
  4. /** 
  5.  * 要求创建三个线程,输出1-75, 
  6.  * 最开始第一个线程输出1-5,第二个输出6-10,第三个输出11-15 
  7.  * 接着再第一个线程输出16-20...就这样循环下去,直到打印出75个数 
  8.  * @author qiaoxueshi 
  9.  * 
  10.  */  
  11. public class Print1to75 {  
  12.     static class Printer implements Runnable{  
  13.         static int num = 1//开始数字  
  14.         static final int END = 75;  
  15.         int id;  
  16.         Object o; //这就是三个线程的“公证人”,有点寒酸吧  
  17.           
  18.         public Printer(int id, Object o) {  
  19.             this.id = id;  
  20.             this.o = o;  
  21.         }  
  22.           
  23.         @Override  
  24.         public void run(){  
  25.             synchronized (o) {  
  26.                 while(num <= END){  
  27.                     if(num / 5 % 3 == id){ //如果是属于自己的数,依次打印出来五个  
  28.                         System.out.print(id + ":");  
  29.                         for(int i = 0; i < 5; i++){  
  30.                             System.out.print(num++ + ", ");  
  31.                         }  
  32.                         System.out.println();  
  33.                           
  34.                         o.notifyAll();//放弃CPU使用权,唤醒在o对象的等待队列上的线程  
  35.                     }else{  
  36.                         try {  
  37.                             o.wait(); //如果不属于自己的数,把当前线程挂在o这个对象的等待队列上(也放弃了CPU使用权),等待唤醒  
  38.                         } catch (InterruptedException e) {  
  39.                             System.out.println("id" + "被打断了");  
  40.                         }  
  41.                     }  
  42.                 }  
  43.             }  
  44.         }  
  45.     }  
  46.       
  47.       
  48.     public static void main(String[] args) {  
  49.         //下面可以不按0,1,2的顺序来,而且在两两中间随便sleep(),都会正确打印出来  
  50.         Object o = new Object();  
  51.         new Thread( new Printer(0, o)).start();  
  52.         new Thread( new Printer(1, o)).start();  
  53.         new Thread( new Printer(2, o)).start();  
  54.     }  
  55. }  
 

 

在第16行,添加了三个线程的”公证人“ Object o;

第25,34,37行都由原来的Printer.class改为了o;

在第50行,创建了一个Object对象,传给了三个线程。

运行结果和上面的是一模一样地!

 

 

Java代码   收藏代码
  1. new Thread( new Printer(0, o)).start();  
  2. new Thread( new Printer(1, o)).start();  
  3. new Thread( new Printer(2, o)).start();  

 如果觉得这段不太优雅,可以使用ExecuorService来实现,道理是一样的。


猜你喜欢

转载自blog.csdn.net/qq_18433441/article/details/78232288