三个线程交替打印ABC

public class PrintABC {
    private static final Object lock = new Object();
    private static volatile Integer index = 0;
    private static final int count = 3;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock) {
                    while (index % count != 0) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {

                        }
                    }

                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {

                    }
                    System.out.print(i + "A");
                    index++;
                    lock.notifyAll();
                }
            }
        }, "a-thread");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock) {
                    while (index % count != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {

                        }
                    }
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {

                    }
                    System.out.print("B");
                    index++;
                    lock.notifyAll();
                }
            }
        }, "b-thread");

        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock) {
                    while (index % count != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {

                        }
                    }
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {

                    }
                    System.out.print("C");
                    index++;
                    lock.notifyAll();
                }
            }
        }, "c-thread");
        t3.start();
        t2.start();
        t1.start();
    }
}

使用ReentrantLock的队列实现的版本

public class PrintABC2 {
    private static volatile Integer COUNT = 0;

    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        Thread t1 = new Thread(() -> {
            // 代表一直打印
            for (; ; ) {
                lock.lock();
                while (COUNT % 3 != 0) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {

                    }
                }

                System.out.print("A");

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {

                }
                COUNT++;
                condition.signalAll();
                lock.unlock();
            }
        });

        Thread t2 = new Thread(() -> {
            // 代表一直打印
            for (; ; ) {
                lock.lock();
                while (COUNT % 3 != 1) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {

                    }
                }

                System.out.print("B");

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {

                }
                COUNT++;
                condition.signalAll();
                lock.unlock();
            }
        });

        Thread t3 = new Thread(() -> {
            // 代表一直打印
            for (; ; ) {
                lock.lock();
                while (COUNT % 3 != 2) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {

                    }
                }

                System.out.print("C");

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {

                }
                COUNT++;
                condition.signalAll();
                lock.unlock();
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}
posted @ 2024-02-19 11:36  Jonathan1  阅读(15)  评论(0编辑  收藏  举报