三个不同线程顺序打印ABC十种写法【并发编程JUC】

夫陶公清风千古,余又何人,敢称庶几
在这里插入图片描述
个人博客地址:http://www.breez.work


📖写法一:使用信号量Semaphore

public class Foo {

    private Semaphore semaphoreB = new Semaphore(0);
    private Semaphore semaphoreC = new Semaphore(0);

    public Foo() {}

    public void first() throws InterruptedException {
        System.out.println("A");
        semaphoreB.release();
    }

    public void second() throws InterruptedException {
        semaphoreB.acquire();
        System.out.println("B");
        semaphoreC.release();
    }

    public void third() throws InterruptedException {
        semaphoreC.acquire();
        System.out.println("C");
    }
}

📖写法二:加synchronized锁

public class Foo {

    private Object lock = new Object();
    private boolean a;
    private boolean b;

    public Foo() {}

    public void first() throws InterruptedException {
        synchronized (lock) {
            System.out.println("A");
            a = true;
            lock.notifyAll();
        }
    }

    public void second() throws InterruptedException {
        synchronized (lock) {
            while (!a) {
                lock.wait();
            }
            System.out.println("B");
            b = true;
            lock.notifyAll();
        }
    }

    public void third() throws InterruptedException {
        synchronized (lock) {
            while (!b) {
                lock.wait();
            }
            System.out.println("C");
        }
    }
}

📖写法三:使用原子AtomicInteger

public class Foo {

    private AtomicInteger a2 = new AtomicInteger(0);
    private AtomicInteger a3 = new AtomicInteger(0);

    public Foo() {}

    public void first() throws InterruptedException {
        System.out.println("A");
        a2.incrementAndGet();
    }

    public void second() throws InterruptedException {
        while (a2.get() != 1) {}
        System.out.println("B");
        a3.incrementAndGet();
    }

    public void third() throws InterruptedException {
        while (a3.get() != 1) {}
        System.out.println("C");
    }
}

📖写法四:使用队列BlockingQueue

public class Foo {

    BlockingQueue<String> queue2 = new ArrayBlockingQueue<>(1);
    BlockingQueue<String> queue3 = new ArrayBlockingQueue<>(1);
    
    public Foo() {}

    public void first() throws InterruptedException {
        System.out.println("A");
        queue2.offer("b");
    }

    public void second() throws InterruptedException {
        while (queue2.size() == 0) {
        }
        System.out.println("B");
        queue3.offer("c");
    }

    public void third() throws InterruptedException {
        while (queue3.size() == 0) {
        }
        System.out.println("C");
    }
}

📖写法五:使用Condition

public class Foo {

    private int number = 1;
    private Lock lock = new ReentrantLock();
    private Condition a = lock.newCondition();
    private Condition b = lock.newCondition();
    private Condition c = lock.newCondition();

    public Foo() {}

    public void first() throws InterruptedException {
        lock.lock();
        try {
            while (number != 1) {
                a.await();
            }
            System.out.println("A");
            number = 2;
            b.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void second() throws InterruptedException {
        lock.lock();
        try {
            while (number != 2) {
                b.await();
            }
            System.out.println("B");
            number = 3;
            c.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void third() throws InterruptedException {
        lock.lock();
        try {
            while (number != 3) {
                c.await();
            }
            System.out.println("C");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

📖写法六:使用计数器CountDownLatch

public class Foo {

    private CountDownLatch c2 = new CountDownLatch(1);
    private CountDownLatch c3 = new CountDownLatch(1);

    public Foo() {}

    public void first() throws InterruptedException {
        System.out.println("A");
        c2.countDown();
    }

    public void second() throws InterruptedException {
        c2.await();
        System.out.println("B");
        c3.countDown();
    }

    public void third() throws InterruptedException {
        c3.await();
        System.out.println("C");

    }
 }

测试代码

public static void main(String[] args) throws InterruptedException {
        Foo foo = new Foo();
        new Thread(() -> {
            try {
                foo.first();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();
        new Thread(() -> {
            try {
                foo.second();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "B").start();

        new Thread(() -> {
            try {
                foo.third();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "C").start();
    }

欢迎加入BreezAm技术交流群:3861 35311 【QQ群】
在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星牛君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值