顺序打印ABC算法题

顺序打印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 }

 

posted @ 2020-05-25 18:44  seedss  阅读(428)  评论(0编辑  收藏  举报