打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
(柯昌合)启动三个线程A,B,C,打印10次 按照ABC的顺序输出(柯昌合)
  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. }  


  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
柯以敏<想你想疯了>等(诺亚之方舟辑)
柯九思
柯九思:《秀石疏竹晚寂》
柯拉
2017,还是得好看下去
柯金胜
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服