建立三个线程,A线程打印10次A,B线程打印10次B,C线程打印10次C。要求线程同时运行,交替打印10次ABC,写出实现代码。

 LockSupport方法

public class LockSupport_Main {
    private static Thread a, b, c;
    public static void main(String[] args) {
        a = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                LockSupport.park();
                System.out.println("A");
                LockSupport.unpark(b);
            }
        });
        b = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                LockSupport.park();
                System.out.println("B");
                LockSupport.unpark(c);
            }
        });
        c = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                LockSupport.park();
                System.out.println("C");
                LockSupport.unpark(a);
            }
        });
        a.start();
        b.start();
        c.start();
        LockSupport.unpark(a);
    }
}

synchronized + wait()/notifyAll() + while(标志位)方法

public class Synchronized_Main {
    private static final Object lock = new Object();
    private static final boolean[] run = {true, false, false};

    public static void main(String[] args) {

        Thread a = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (lock) {
                        while (!run[0]) lock.wait();
                        System.out.println("A");
                        run[0] = false;
                        run[1] = true;
                        lock.notifyAll();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread b = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (lock) {
                        while (!run[1]) lock.wait();
                        System.out.println("B");
                        run[1] = false;
                        run[2] = true;
                        lock.notifyAll();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread c = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (lock) {
                        while (!run[2]) lock.wait();
                        System.out.println("C");
                        run[2] = false;
                        run[0] = true;
                        lock.notifyAll();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        a.start();
        b.start();
        c.start();
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值