每日一问 - 三个线程交替打印 ABCABC

每日一问 - 三个线程交替打印 ABCABC…

问题

三个线程交替打印 ABCABC.. ?
三个线程交替打印 ABCABC.. 10 次?

解决方案

三个线程交替打印 ABCABC…

创建共享资源 :

/**
 * wait notify 实现 ABC交替打印
 */
public class ABCCoreHolder {

    private int counter = 0;

    public void printA() throws InterruptedException {
        synchronized (this) {
            //不用if, 防止操作系统虚度唤醒
            while (counter % 3 != 0) {
                wait();
            }

            System.out.print("A");
            counter++;
            TimeUnit.MILLISECONDS.sleep(500);
            //三个以上线程一定要用 notifyAll
            notifyAll();
        }
    }

    public void printB() throws InterruptedException {
        synchronized (this) {
            while (counter % 3 != 1) {
                wait();
            }
            System.out.print("B");
            counter++;
            TimeUnit.MILLISECONDS.sleep(500);
            //三个以上线程一定要用 notifyAll
            notifyAll();
        }
    }

    public void printC() throws InterruptedException {
        synchronized (this) {
            while (counter % 3 != 2) {
                wait();
            }

            System.out.print("C");
            counter++;
            TimeUnit.MILLISECONDS.sleep(500);
            //三个以上线程一定要用 notifyAll
            notifyAll();
        }
    }


}
测试用例
public static void abcLoopTest() {
    final ABCCoreHolder holder = new ABCCoreHolder();
    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    holder.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    holder.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    Thread t3 = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    holder.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    t1.start();
    t2.start();
    t3.start();
}

结果:

ABCABCABCABCABCABCABC...
三个线程交替打印 ABCABC… 10 次
public static void abcLoop10Test() {
    final ABCCoreHolder holder = new ABCCoreHolder();
    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    holder.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    holder.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    Thread t3 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    holder.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    t1.start();
    t2.start();
    t3.start();
}

结果:

ABCABCABCABCABCABCABCABCABCABC
Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值