多线程交替打印ABC

开启3个线程,依次交替打印ABC,打印5轮

一、使用Lock+Condition来实现

public class ThreadABC {
    private volatile int number = 1;
    private Lock lock = new ReentrantLock();
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();


    public void printA() {
        lock.lock();
        try {
            while (number != 1) {
                //使当前线程释放锁,暂时wait挂起,直到下次被signal后唤醒
                c1.await();
            }
            System.out.println("A");
            number = 2;
            //唤醒c2
            c2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printB() {
        lock.lock();
        try {
            while (number != 2) {
                c2.await();
            }
            System.out.println("B");
            number = 3;
            c3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printC() {
        lock.lock();
        try {
            while (number != 3) {
                c3.await();
            }
            System.out.println("C");
            number = 1;
            c1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        ThreadABC threadABC = new ThreadABC();
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                threadABC.printA();
            }
        }, "1").start();
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                threadABC.printB();
            }
        }, "2").start();
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                threadABC.printC();
            }
        }, "3").start();
    }
}

二、使用Semaphore实现

class ThreadABC1 {

    Semaphore s1 = new Semaphore(1);
    Semaphore s2 = new Semaphore(1);
    Semaphore s3 = new Semaphore(1);

    public static void main(String[] args) {
        ThreadABC1 threadABC1 = new ThreadABC1();
        try {
            threadABC1.s2.acquire();
            threadABC1.s3.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            try {
                for (int i = 0; i < 5; i++) {
                    //acquire成功后,许可数量减一
                    //如果acquire不成功,持有该信号量的当前线程就会处于休眠状态
                    threadABC1.s1.acquire();
                    System.out.println("A");
                    //release之后,持有该信号量的线程就会被唤醒
                    threadABC1.s2.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        new Thread(() -> {
            try {
                for (int i = 0; i < 5; i++) {
                    threadABC1.s2.acquire();
                    System.out.println("B");
                    threadABC1.s3.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        new Thread(() -> {
            try {
                for (int i = 0; i < 5; i++) {
                    threadABC1.s3.acquire();
                    System.out.println("C");
                    threadABC1.s1.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();
    }
}

  

posted @ 2024-12-15 13:12  达摩克利斯之剑  阅读(33)  评论(0)    收藏  举报