ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

顺序打印ABC算法题

2020-05-25 18:54:12  阅读:180  来源: 互联网

标签:ABC Thread lock 打印 private 算法 ch new public


顺序打印ABCABC...ABC,打印10组。

方法一:使用ReentrantLock和Condition实现。使用三组Condition来实现线程之间的通信。

 1 public class PrintABC implements Runnable {
 2 
 3     private int times = 10;
 4 
 5     private ReentrantLock lock;
 6 
 7     private Condition thisCondition;
 8 
 9     private Condition nextCondition;
10 
11     private char ch;
12 
13     public PrintABC() {
14     }
15 
16     public PrintABC(ReentrantLock lock, Condition thisCondition, Condition nextCondition, char ch) {
17         this.lock = lock;
18         this.thisCondition = thisCondition;
19         this.nextCondition = nextCondition;
20         this.ch = ch;
21     }
22 
23     @Override
24     public void run() {
25         lock.lock();
26         try {
27             for (int i = 0; i < times; i++) {
28                 System.out.println(ch);
29                 nextCondition.signal();
30                 if (i < times - 1) {
31                     thisCondition.await();//此处需要使用判断
32                 }
33             }
34 
35         } catch (InterruptedException e) {
36             e.printStackTrace();
37         } finally {
38             lock.unlock();
39         }
40     }
41 
42     public static void main(String[] args) throws InterruptedException {
43         ReentrantLock lock = new ReentrantLock();
44         Condition conditionA = lock.newCondition();
45         Condition conditionB = lock.newCondition();
46         Condition conditionC = lock.newCondition();
47         PrintABC printA = new PrintABC(lock, conditionA, conditionB, 'A');
48         PrintABC printB = new PrintABC(lock, conditionB, conditionC, 'B');
49         PrintABC printC = new PrintABC(lock, conditionC, conditionA, 'C');
50         new Thread(printA).start();
51         Thread.sleep(100);
52         new Thread(printB).start();
53         Thread.sleep(100);
54         new Thread(printC).start();
55         Thread.sleep(100);
56     }
57 }

方法二:使用Object对象作为共享对象,volatile status作为同步打印ABC的判断条件。

 1 public class PrintABC1 {
 2 
 3     private volatile int status = 0;
 4 
 5     private class PrintStr implements Runnable{
 6         private int times = 10;
 7 
 8         private Object lock;
 9 
10         private int thisStatus;
11 
12         private int nextStatus;
13 
14         private char ch;
15 
16         public PrintStr(Object lock, int thisStatus, int nextStatus, char ch){
17             this.lock = lock;
18             this.thisStatus = thisStatus;
19             this.nextStatus = nextStatus;
20             this.ch = ch;
21         }
22 
23         @SneakyThrows
24         @Override
25         public void run() {
26             synchronized (lock) {
27                 for (int i=0; i < times; i++) {
28                     while (status != thisStatus) {
29                         lock.wait();
30                     }
31                     System.out.println(ch);
32                     status = nextStatus;
33                     lock.notifyAll();
34                 }
35             }
36         }
37     }
38 
39     public static void main(String[] args) throws InterruptedException {
40 
41         PrintABC1 printABC1 = new PrintABC1();
42         printABC1.test();
43     }
44 
45     public void test() throws InterruptedException {
46         Object lock = new Object();
47         PrintStr printStrA = new PrintStr(lock, 0, 1, 'A');
48         PrintStr printStrB = new PrintStr(lock, 1, 2, 'B');
49         PrintStr printStrC = new PrintStr(lock, 2, 0, 'C');
50         new Thread(printStrA).start();
51         Thread.sleep(100);
52         new Thread(printStrB).start();
53         Thread.sleep(100);
54         new Thread(printStrC).start();
55     }
56 }

 

标签:ABC,Thread,lock,打印,private,算法,ch,new,public
来源: https://www.cnblogs.com/seedss/p/12960275.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有