顺序打印ABC算法题

Posted seedss

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顺序打印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 }

 

以上是关于顺序打印ABC算法题的主要内容,如果未能解决你的问题,请参考以下文章

多线程面试题之三线程按顺序交替打印ABC的方法

java多线程编程之连续打印abc的几种解法

算法题:二叉树打印

多线程顺序打印ABC

算法题:打印1到最大的n位数

java算法面试题:有一个字符串,其中包含中文字符英文字符和数字字符,请统计和打印出各个字符的个数 按值的降序排序,如果值相同则按键值的字母顺序