交替打印ABC的实现

实现方案

方案一:使用LockSupport

package com.jesse.review.test4;

import java.util.concurrent.locks.LockSupport;

/**
 * Created by Kong on 2021/1/13.
 */
public class Test41 {
    static Thread threadA, threadB, threadC;

    public static void main(String[] args) {
        threadA = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.print(Thread.currentThread().getName());
                LockSupport.unpark(threadB);
                LockSupport.park();
            }
        }, "A");

        threadB = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                LockSupport.park();
                System.out.print(Thread.currentThread().getName());
                LockSupport.unpark(threadC);
            }
        }, "B");

        threadC = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                LockSupport.park();
                System.out.print(Thread.currentThread().getName());
                LockSupport.unpark(threadA);
            }
        }, "C");

        threadA.start();
        threadB.start();
        threadC.start();
    }
}

方案二:使用synchronized

package com.jesse.review.test4;

/**
 * Created by Kong on 2021/1/13.
 */
public class Test42 {
    private static boolean startA = true;
    private static boolean startB = false;
    private static boolean startC = false;

    public static void main(String[] args) {
        final Object o = new Object();

        new Thread(() -> {
            synchronized (o){
                for(int i = 0; i < 10;){
                    if(startA){
                        System.out.print(Thread.currentThread().getName());
                        startA = false;
                        startB = true;
                        startC = false;
                        o.notifyAll();
                        i++;
                    }else {
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "A").start();

        new Thread(() -> {
            synchronized (o){
                for(int i = 0; i < 10;){
                    if(startB){
                        System.out.print(Thread.currentThread().getName());
                        startA = false;
                        startB = false;
                        startC = true;
                        o.notifyAll();
                        i++;
                    }else {
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "B").start();

        new Thread(() -> {
            synchronized (o){
                for(int i = 0; i < 10;){
                    if(startC){
                        System.out.print(Thread.currentThread().getName());
                        startA = true;
                        startB = false;
                        startC = false;
                        o.notifyAll();
                        i++;
                    }else {
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "C").start();
    }
}

方案三:使用Lock搭配Condition实现

package com.jesse.review.test4;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Kong on 2021/1/13.
 */
public class Test43 {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();

        Condition conditionA = lock.newCondition();
        Condition conditionB = lock.newCondition();
        Condition conditionC = lock.newCondition();

        new Thread(() -> {
            try{
                lock.lock();
                for (int i = 0; i < 10; i++) {
                    System.out.print(Thread.currentThread().getName());
                    conditionB.signal();
                    conditionA.await();
                }
                conditionB.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"A").start();

        new Thread(() -> {
            try{
                lock.lock();
                for (int i = 0; i < 10; i++) {
                    System.out.print(Thread.currentThread().getName());
                    conditionC.signal();
                    conditionB.await();
                }
                conditionC.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"B").start();

        new Thread(() -> {
            try{
                lock.lock();
                for (int i = 0; i < 10; i++) {
                    System.out.print(Thread.currentThread().getName());
                    conditionA.signal();
                    conditionC.await();
                }
                conditionA.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"C").start();
    }
}

转载

上一篇:Java 并发编程之同步工具类信号量 Semaphore


下一篇:CompletableFuture 获取所有task的结果