【面试题】多线程交替打印

这两天在知乎上看到不同的人都问了这个问题,回想起当年找实习的时候也写过这个问题,觉得还蛮有意思的,就在这里记录一下。
最常见的是使用 notify() wait()

public class Demo {
    public static void main(String[] args) {
        Object lock = new Object();
        new Printer("线程 A", lock).start();
        new Printer("线程 B", lock).start();
    }

    private static class Printer extends Thread {
        private final Object lock;

        public Printer(String name, Object lock) {
            super(name);
            this.lock = lock;
        }

        @Override
        public void run() {
            for (char i = 'A'; i <= 'Z'; i++) {
                synchronized (lock) {
                    System.out.println(getName() + " 打印字母: " + i);
                    lock.notify();
                    try {
                        Thread.sleep(500);
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

不使用锁

public class Demo {

    /**
     * 使用计数器,两个线程分别在计数器为奇数/偶数时打印,
     * 使用AtomicBoolean更简单,这里使用AtomicInteger
     * 是考虑可拓展为N个线程交替打印
     */
    public static void main(String[] args) {
        AtomicInteger counter = new AtomicInteger(0);
        new PrinterA("线程 A", counter).start();
        new PrinterB("线程 B", counter).start();
    }

    private static class PrinterA extends Thread {
        private final AtomicInteger counter;

        public PrinterA(String name, AtomicInteger counter) {
            super(name);
            this.counter = counter;
        }

        @Override
        public void run() {
            for (char i = 'A'; i <= 'Z'; ) {
                if (counter.get() % 2 == 0) {
                    System.out.println(getName() + " 打印字母: " + i++);
                    counter.incrementAndGet();
                }
            }
        }
    }

    private static class PrinterB extends Thread {
        private final AtomicInteger counter;

        public PrinterB(String name, AtomicInteger counter) {
            super(name);
            this.counter = counter;
        }

        @Override
        public void run() {
            for (char i = 'A'; i <= 'Z'; ) {
                if (counter.get() % 2 != 0) {
                    System.out.println(getName() + " 打印字母: " + i++);
                    counter.incrementAndGet();
                }
            }
        }
    }
}

使用 ReentrantLock

public class Demo {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition aPrintCondition = lock.newCondition();
        Condition bPrintCondition = lock.newCondition();
        Condition cPrintCondition = lock.newCondition();
        new Printer("线程 A", lock, aPrintCondition, bPrintCondition).start();
        new Printer("线程 B", lock, bPrintCondition, cPrintCondition).start();
        new Printer("线程 C", lock, cPrintCondition, aPrintCondition).start();
    }

    private static class Printer extends Thread {
        private final ReentrantLock lock;
        private final Condition waitCondition;
        private final Condition signalCondition;

        public Printer(String name, ReentrantLock lock, Condition waitCondition, Condition signalCondition) {
            super(name);
            this.lock = lock;
            this.waitCondition = waitCondition;
            this.signalCondition = signalCondition;
        }

        @Override
        public void run() {
            for (char i = 'A'; i <= 'Z'; i++) {
                lock.lock();
                try {
                    System.out.println(getName() + " 打印字母: " + i);
                    Thread.sleep(500);
                    signalCondition.signal();
                    waitCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }
}

还可以使用其他juc包里的同步类 Semaphore 等等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值