面试题 多线程并发 两个线程交替打印

public class Test2 {
    private static int count = 0;
    private final static Object lock = new Object();
    static class TurningRunner implements Runnable {
        @Override
        public void run() {
            while (count <= 10) {
                // 获取锁
                synchronized (lock) {
                    // 拿到锁就打印
                    System.out.println(Thread.currentThread().getName() + ":" + count++);
                    // 唤醒其他线程
                    lock.notifyAll();
                    try {
                        // 如果任务还没有结束,则让出当前的锁并休眠
                        if (count <= 10) {
                            lock.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread a = new Thread(new TurningRunner(), "偶数");
        Thread b = new Thread(new TurningRunner(), "奇数");
        a.start();
        b.start();
    }
}

猜你喜欢

转载自www.cnblogs.com/yanhowever/p/12336744.html