Java中采用3个线程不停的轮流打印ABC这3个字符,采用不同的方式实现。

原理分析

实现思路如下:

  • 要在一个while循环中,实现不停的打印
  • 多个线程间需要依次唤醒下一个线程,实现依次打印,此时会形成死循环
  • 为了打破死循环,还需要一个启动线程延后调用

原理图

代码实现

基于wait&notify

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class ThreadPrint1Test {

    public static void main(String[] args) {
        new ThreadPrint1Test().testPrint();
    }

    public void testPrint() {
        Object lockA = new Object();
        Object lockB = new Object();
        Object lockC = new Object();

        new PrintThread("A", lockA, lockB).start();
        new PrintThread("B", lockB, lockC).start();
        new PrintThread("C", lockC, lockA).start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(() -> {
            synchronized (lockA) {
                lockA.notify();
            }
        }).start();
    }

    class PrintThread extends Thread {

        private Object waitLock;
        private Object notifyLock;
        private String value;

        public PrintThread(String value, Object waitLock, Object notifyLock) {
            this.value = value;
            this.waitLock = waitLock;
            this.notifyLock = notifyLock;
        }

        public void run() {
            while (true) {
                synchronized (waitLock) {
                    try {
                        waitLock.wait();
                        Thread.sleep(500);
                        System.out.println(value);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    synchronized (notifyLock) {
                        notifyLock.notify();
                    }
                }
            }
        }
    }
}

基于ReentrantLock&Condition

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class ThreadPrint2Test {

    public static void main(String[] args) {
        new ThreadPrint2Test().testPrint();
    }

    public void testPrint() {
        Lock lock = new ReentrantLock();
        Condition conditionA = lock.newCondition();
        Condition conditionB = lock.newCondition();
        Condition conditionC = lock.newCondition();

        new Thread(new PrintThread("A", lock, conditionA, conditionB), "thread-a").start();
        new Thread(new PrintThread("B", lock, conditionB, conditionC), "thread-b").start();
        new Thread(new PrintThread("C", lock, conditionC, conditionA), "thread-c").start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(() -> {
            lock.lock();
            conditionA.signal();
            lock.unlock();
        }, "init").start();
    }

    class PrintThread implements Runnable {

        private Lock lock;
        private Condition ca;
        private Condition cb;
        private String value;

        public PrintThread(String value, Lock lock, Condition ca, Condition cb) {
            this.value = value;
            this.lock = lock;
            this.ca = ca;
            this.cb = cb;
        }

        public void run() {
            while (true) {
                try {
                    lock.lock();
                    ca.await();
                    System.out.println(value);
                    Thread.sleep(500);
                    cb.signal();
                    lock.unlock();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

基于Semaphore

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class ThreadPrint3Test {

    public static void main(String[] args) {
        new ThreadPrint3Test().testPrint();
    }

    public void testPrint() {
        Semaphore sa = new Semaphore(1);
        Semaphore sb = new Semaphore(1);
        Semaphore sc = new Semaphore(1);

        try {
            sb.acquire();
            sc.acquire();
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new PrintThread("A", sa, sb).start();
        new PrintThread("B", sb, sc).start();
        new PrintThread("C", sc, sa).start();
    }

    class PrintThread extends Thread {

        private Semaphore sa;
        private Semaphore sb;
        private String value;

        public PrintThread(String value, Semaphore sa, Semaphore sb) {
            this.value = value;
            this.sa = sa;
            this.sb = sb;
        }

        public void run() {
            while (true) {
                try {
                    sa.acquire();
                    System.out.println(value);
                    Thread.sleep(500);
                    sb.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

单个锁&volatile变量

此种方式不需要唤醒线程,同时便于很方便的修改线程数目。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class ThreadPrint4Test {

    public static void main(String[] args) {
        new ThreadPrint4Test().testPrint();
    }

    private volatile int count = 0;

    public void testPrint() {
        Object lock = new Object();
        new Thread(new PrintThread("A", lock), "thread-A").start();
        new Thread(new PrintThread("B", lock), "thread-B").start();
        new Thread(new PrintThread("C", lock), "thread-C").start();
    }

    class PrintThread implements Runnable {

        private Object lock;
        private String value;

        public PrintThread(String value, Object lock) {
            this.value = value;
            this.lock = lock;
        }

        public void run() {
            while (true) {
                try {
                    synchronized (lock) {
                        boolean process = "A".equals(value) && count % 3 == 0;
                        if (process) {
                            count = 0;
                        }
                        process = process || ("B".equals(value) && count % 3 == 1);
                        process = process || ("C".equals(value) && count % 3 == 2);
                        if (process) {
                            lock.wait();
                            System.out.println(value);
                            count++;
                            Thread.sleep(500);
                        }
                        lock.notifyAll();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}