分享

(柯昌合)启动三个线程A,B,C,打印10次 按照ABC的顺序输出(柯昌合)

 大头祥子 2014-02-11
  1. import java.util.concurrent.ExecutorService;  
  2. import java.util.concurrent.Executors;  
  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 ABC5 {  
  8.   
  9.     private int cond = 1;// 通过cond来确定A B C的输出  
  10.     private Lock lock = new ReentrantLock();// 通过JDK5中的锁来保证线程的访问的互斥  
  11.     private Condition condition = lock.newCondition();// 线程协作  
  12.   
  13.     public static void main(String[] args) {  
  14.         ABC5 abc = new ABC5();// 内部类线程执行方式jdk1.5  
  15.         ThreadA ta = abc.new ThreadA();// 声明3个runnable类  
  16.         ThreadB tb = abc.new ThreadB();  
  17.         ThreadC tc = abc.new ThreadC();  
  18.         ExecutorService executor = Executors.newFixedThreadPool(3);// 通过线程池执行  
  19.         for (int i = 0; i < 10; i++) {  
  20.             executor.execute(ta);  
  21.             executor.execute(tb);  
  22.             executor.execute(tc);  
  23.         }  
  24.         executor.shutdown();// 关闭线程池  
  25.   
  26.     }  
  27.   
  28.     class ThreadA implements Runnable {  
  29.   
  30.         public void run() {  
  31.             lock.lock();  
  32.             try {  
  33.                 while (true) {  
  34.                     if (cond % 3 == 1) {  
  35.                         System.out.print(cond+":A");  
  36.                         cond++;  
  37.                         condition.signalAll();  
  38.                         break;  
  39.                     } else {  
  40.                         try {  
  41.                             condition.await();  
  42.                         } catch (InterruptedException e) {  
  43.                             e.printStackTrace();  
  44.                         }  
  45.                     }  
  46.                 }  
  47.             } finally {  
  48.                 lock.unlock();  
  49.             }  
  50.         }  
  51.   
  52.     }  
  53.   
  54.     class ThreadB implements Runnable {  
  55.         public void run() {  
  56.             lock.lock();  
  57.             try {  
  58.                 while (true) {  
  59.                     if (cond % 3 == 2) {  
  60.                         System.out.print(cond+":B");  
  61.                         cond++;  
  62.                         condition.signalAll();  
  63.                         break;  
  64.                     } else {  
  65.                         try {  
  66.                             condition.await();  
  67.                         } catch (InterruptedException e) {  
  68.                             e.printStackTrace();  
  69.                         }  
  70.                     }  
  71.                 }  
  72.             } finally {  
  73.                 lock.unlock();  
  74.             }  
  75.         }  
  76.   
  77.     }  
  78.   
  79.     class ThreadC implements Runnable {  
  80.         public void run() {  
  81.             lock.lock();  
  82.             try {  
  83.                 while (true) {  
  84.                     if (cond % 3 == 0) {  
  85.                         System.out.println(cond+":C");  
  86.                         cond++;  
  87.                         condition.signalAll();  
  88.                         break;  
  89.                     } else {  
  90.                         try {  
  91.                             condition.await();  
  92.                         } catch (InterruptedException e) {  
  93.                             e.printStackTrace();  
  94.                         }  
  95.                     }  
  96.                 }  
  97.             } finally {  
  98.                 lock.unlock();  
  99.             }  
  100.         }  
  101.   
  102.     }  
  103.   
  104. }  


  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约