通过线程按照顺序循环输出ABC n次

通过线程按照顺序循环输出ABC n次

根据题目的意思应该是每个线程输出一个字符,按照顺序输出n次。

对于这个题目应该是3个线程。

分析:线程的执行方式是乱序的,需要通过协作才能实现3个线程顺序输出字符。

在jdk1.4中线程的协作是通过wait/notify/notifyAll实现的,使用这3个方法要求获取同步对象的monitor,否则即使编译成功也会在运行的时候出现 IllegalMonitorStateException的异常。

在jdk1.5中线程的协作是通过Lock/Condition及Samephore/CyclicBarriar/CountLatchDown实现的

 

JDK1.4的实现

 

[java]  view plain copy
  1. package demo;  
  2.   
  3. public class ABC4 {  
  4.     private int cond = 1;//通过cond来确定A B C的输出  
  5.     private Object obj = new Object();//同步对象  
  6.     /** 
  7.      * @param args 
  8.      */  
  9.     public static void main(String[] args) {  
  10.         ABC4 abc = new ABC4();//内部类,线程执行通过jdk1.4  
  11.         ThreadA ta = abc.new ThreadA();//声明3个runnable类  
  12.         ThreadB tb = abc.new ThreadB();  
  13.         ThreadC tc = abc.new ThreadC();  
  14.         for (int i = 0; i < 10; i++) {  
  15.             new Thread(ta).start();  
  16.             new Thread(tb).start();  
  17.             new Thread(tc).start();  
  18.         }  
  19.     }  
  20.   
  21.     class ThreadA implements Runnable{  
  22.   
  23.         public void run() {  
  24.             synchronized (obj) {  
  25.                 while(true){  
  26.                     if (cond % 3 == 1) {  
  27.                         System.out.println("A");  
  28.                         cond++;  
  29.                         obj.notifyAll();  
  30.                         break;  
  31.                     }  
  32.                     else {  
  33.                         try {  
  34.                             obj.wait();  
  35.                         } catch (InterruptedException e) {  
  36.                             e.printStackTrace();  
  37.                         }  
  38.                     }  
  39.                 }  
  40.             }  
  41.         }  
  42.           
  43.     }  
  44.     class ThreadB implements Runnable{  
  45.   
  46.         public void run() {  
  47.             synchronized (obj) {  
  48.                 while(true){  
  49.                     if (cond % 3 == 2) {  
  50.                         System.out.println("B");  
  51.                         cond++;  
  52.                         obj.notifyAll();  
  53.                         break;  
  54.                     }  
  55.                     else {  
  56.                         try {  
  57.                             obj.wait();  
  58.                         } catch (InterruptedException e) {  
  59.                             e.printStackTrace();  
  60.                         }  
  61.                     }  
  62.                 }  
  63.             }  
  64.         }  
  65.           
  66.     }  
  67.     class ThreadC implements Runnable{  
  68.   
  69.         public void run() {  
  70.             synchronized (obj) {  
  71.                 while(true){  
  72.                     if (cond % 3 == 0) {  
  73.                         System.out.println("C");  
  74.                         cond++;  
  75.                         obj.notifyAll();  
  76.                         break;  
  77.                     }  
  78.                     else {  
  79.                         try {  
  80.                             obj.wait();  
  81.                         } catch (InterruptedException e) {  
  82.                             e.printStackTrace();  
  83.                         }  
  84.                     }  
  85.                 }  
  86.             }  
  87.         }  
  88.           
  89.     }  
  90. }  

 

JDK1.5的实现

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值