Java高频面试:3个线程循环n次,每次分别输出A、B、C

前言:假设有3个线程,循环5次,每次各个线程依次输出A、B、C
如下:

thread_1 : >>> A
thread_2 : >>> B
thread_3 : >>> C
thread_1 : >>> A
thread_2 : >>> B
thread_3 : >>> C
thread_1 : >>> A
thread_2 : >>> B
thread_3 : >>> C
thread_1 : >>> A
thread_2 : >>> B
thread_3 : >>> C
thread_1 : >>> A
thread_2 : >>> B
thread_3 : >>> C

这里给出了2中写法

方法一

 /**
  * 3个线程循环 CYCLE_NUM 次,分别输出 A、B、C
  */
 private static void printCharBy3Threads() {
     Semaphore aSemaphore = new Semaphore(1);
     Semaphore bSemaphore = new Semaphore(0);
     Semaphore cSemaphore = new Semaphore(0);
     int CYCLE_NUM = 4;
     Thread threadA = new Thread("thread_1") {
         @Override
         public void run() {
             try {
                 for (int i = 0; i < CYCLE_NUM; i++) {
                     aSemaphore.acquire();
                     System.out.println(getName() + " : >>> A");
                     bSemaphore.release();
                 }
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
     };
     Thread threadB = new Thread("thread_2") {
         @Override
         public void run() {
             try {
                 for (int i = 0; i < CYCLE_NUM; i++) {
                     bSemaphore.acquire();
                     System.out.println(getName() + " : >>> B");
                     cSemaphore.release();
                 }
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
     };
     Thread threadC = new Thread("thread_3") {
         @Override
         public void run() {
             try {
                 for (int i = 0; i < CYCLE_NUM; i++) {
                     cSemaphore.acquire();
                     System.out.println(getName() + " : >>> C");
                     aSemaphore.release();
                 }
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
     };
     threadA.start();
     threadB.start();
     threadC.start();
 }

方法二
和方法一 区别不大,就是封装了下 Thread,如下:

static class PrintCharThread implements Runnable {
    private int cycle_num;
    private Semaphore curSemaphore;
    private Semaphore nextSemaphore;
    private AtomicInteger value;
    private int INTERVAL = 3;
    public PrintCharThread(int cycle_num, Semaphore curSemaphore, Semaphore nextSemaphore, AtomicInteger value) {
        this.cycle_num = cycle_num;
        this.curSemaphore = curSemaphore;
        this.nextSemaphore = nextSemaphore;
        this.value = value;
    }
    @Override
    public void run() {
        try {
            for (int i = 0; i < cycle_num; i++) {
                curSemaphore.acquire();
                if (value.get() % INTERVAL == 1) {
                    System.out.println(Thread.currentThread().getName() + " : >>> A");
                } else if (value.get() % INTERVAL == 2) {
                    System.out.println(Thread.currentThread().getName() + " : >>> B");
                } else {
                    System.out.println(Thread.currentThread().getName() + " : >>> C");
                }
                value.getAndAdd(1);
                nextSemaphore.release();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

调用方法如下:

private static void printCharBy3Threads2() {
    int CYCLE_NUM = 5;
    AtomicInteger value = new AtomicInteger(1);
    Semaphore aSemaphore = new Semaphore(1);
    Semaphore bSemaphore = new Semaphore(0);
    Semaphore cSemaphore = new Semaphore(0);
    Thread threadA = new Thread(new PrintCharThread(CYCLE_NUM, aSemaphore, bSemaphore, value));
    Thread threadB = new Thread(new PrintCharThread(CYCLE_NUM, bSemaphore, cSemaphore, value));
    Thread threadC = new Thread(new PrintCharThread(CYCLE_NUM, cSemaphore, aSemaphore, value));
    threadA.setName("thread_1");
    threadA.start();
    threadB.setName("thread_2");
    threadB.start();
    threadC.setName("thread_3");
    threadC.start();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值