一道线程的面试题

时间:2014-05-01 19:25:26   收藏:0   阅读:376

线程1循环3次,线程2循环6次,线程3循环9次,接着又回到线程1循环3次,线程2循环6次,线程3循环9次...如此循环50次

mamicode.com,码迷
  1 package ch03;
  2 
  3 import java.util.concurrent.locks.Condition;
  4 import java.util.concurrent.locks.Lock;
  5 import java.util.concurrent.locks.ReentrantLock;
  6 
  7 public class ConditionCommunication {
  8 
  9     public static void main(String[] args) {
 10         final Business business = new Business();
 11         //线程1
 12         new Thread(new Runnable() {
 13             
 14             @Override
 15             public void run() {
 16                 for(int i=1; i<=50; i++){
 17                     business.sub1(i);
 18                 }
 19             }
 20         }).start();
 21         
 22         //线程2
 23         new Thread(new Runnable() {
 24             
 25             @Override
 26             public void run() {
 27                 for(int i=1; i<=50; i++){
 28                     business.sub2(i);
 29                 }
 30             }
 31         }).start();
 32         
 33         //主线程
 34         for(int i=1; i<=50; i++){
 35             business.sub3(i);
 36         }
 37         
 38     }
 39 
 40 }
 41 
 42 /**
 43  * 将相似的业务代码封装在同一个类中
 44  *
 45  */
 46 class Business{
 47     
 48     private Lock lock = new ReentrantLock();
 49     //标志位,1:表示线程1执行;2表示线程2执行;3表示线程3执行
 50     volatile int flag = 1; 
 51     //条件
 52     private Condition condition1 = lock.newCondition();
 53     private Condition condition2 = lock.newCondition();
 54     private Condition condition3 = lock.newCondition();
 55     
 56     public void sub1(int i){
 57         lock.lock();
 58         
 59         while(flag != 1){
 60             try {
 61                 condition1.await(); //线程1等待
 62             } catch (InterruptedException e) {
 63                 e.printStackTrace();
 64             }
 65         }
 66         for(int j=1; j<=3; j++){
 67             System.out.println("sub1 "+j+",loop "+i);
 68         }
 69         flag = 2;
 70         //唤醒线程2
 71         condition2.signal();
 72         
 73         lock.unlock();
 74     }
 75     
 76     public void sub2(int i){
 77         lock.lock();
 78         
 79         while(flag != 2){
 80             try {
 81                 condition2.await();    //线程2等待
 82             } catch (InterruptedException e) {
 83                 e.printStackTrace();
 84             }
 85         }
 86         for(int j=1; j<=6; j++){
 87             System.out.println("sub2  执行"+j+"次,外部循环"+i);
 88         }
 89         flag = 3;
 90         //唤醒线程3
 91         condition3.signal();
 92         
 93         lock.unlock();
 94     }
 95     
 96     public void sub3(int i){
 97         lock.lock();
 98         
 99         while(flag != 3){
100             try {
101                 condition3.await();    //线程3等待
102             } catch (InterruptedException e) {
103                 e.printStackTrace();
104             }
105         }
106         for(int j=1; j<=9;j++){
107             System.out.println("sub3  执行"+j+"次,外部循环............"+i);
108         }
109         flag = 1;
110         //唤醒线程1
111         condition1.signal();
112         
113         lock.unlock();
114     }
115     
116 }
mamicode.com,码迷

 

一道线程的面试题,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!