多线程之交替打印

方法1

public static Thread t1;
    public static Thread t2;
    public static void main(String[] args) {
        char[] cs = {'a','b','c','d'};
        int[] is = {1,2,3,4};
        CountDownLatch countDownLatch = new CountDownLatch(1);
        Object lock = new Object();
        t1 = new Thread(()->{
            try {
            	// 先阻塞 确保先输出数字
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lock) {
                for (int i = 0; i < cs.length; i++) {
                    System.out.println(cs[i]);
                    try {
                        lock.notify();// 唤醒t2
                        lock.wait(); // 阻塞t1
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                lock.notify(); // 避免最后有一个线程一直执行不完
            }
        },"t1");

        t2 = new Thread(()->{
            synchronized (lock) {
                for (int i = 0; i < is.length; i++) {
                    System.out.println(is[i]);
                    // 确保第一个输出的是数字 必须放在输出后
                    countDownLatch.countDown();
                    try {
                        lock.notify();
                        lock.wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                lock.notify();// 避免最后有一个线程一直执行不完
            }
        },"t2");
        t1.start();
        t2.start();
    }

方法2

	public static Thread t1;
    public static Thread t2;
    public static void main(String[] args) {
        char[] cs = {'a','b','c','d'};
        int[] is = {1,2,3,4};
        Lock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        t1 = new Thread(()->{
            lock.lock(); 
            try {
                for (int i = 0; i < cs.length; i++) {
                    System.out.println(cs[i]);
                    condition2.signal(); // 唤醒队列2中的
                    condition1.await();
                }
                condition2.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        },"t1");

        t2 = new Thread(()->{
            lock.lock();
            try {
                for (int i = 0; i < is.length; i++) {
                    System.out.println(is[i]);
                    condition1.signal();
                    condition2.await();
                }
                condition1.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"t2");
        t1.start();
        t2.start();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值