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

Posted 小猪快跑22

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java高频面试:3个线程循环n次,每次分别输出ABC相关的知识,希望对你有一定的参考价值。

前言:假设有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();

以上是关于Java高频面试:3个线程循环n次,每次分别输出ABC的主要内容,如果未能解决你的问题,请参考以下文章

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

记一个Java多线程相关的面试题

Java 高频面试:2个线程分别依次输出数字和字母

JAVA多线程编程,创建3个线程分别打印A,B和C,打印10次

Java多线程:用三个线程控制循环输出10次ABC

大厂高频面试:Java并发篇(线程6种状态线程池悲观锁vs乐观锁等)