您现在的位置是:亿华云 > 应用开发

面试官:三个线程顺序执行,你来说说有几种实现方式?

亿华云2025-10-04 02:39:30【应用开发】6人已围观

简介能想起来几种呢?先说下要求,就是三个线程,假设是线程 1,2,3, 现在的要求是:必须是线程 1 先执行,然后线程 2 再执行,最后是线程 3 执行,然后有几种实现方法呢?其实它的本质就是实现,让线程

能想起来几种呢?面试

先说下要求,就是线程三个线程,假设是顺序说说实现线程 1,2,执行3,有种 现在的面试要求是:必须是线程 1 先执行,然后线程 2 再执行,线程最后是顺序说说实现线程 3 执行,然后有几种实现方法呢?执行

其实它的本质就是实现,让线程 2,有种3 等待线程 1 执行完毕,亿华云面试所以重点就是线程有哪些方法可以让线程 2,3 等待。顺序说说实现

join

第一反应应该就是执行使用 join 方法,因为 join 本来就是有种支持这种机制的

比如,我在线程 B 中调用了线程 A 的 join 方法,那么线程 B 就会等线程 A 执行结束之后再执行

那么具体应该怎么使用嘞?

别慌嘛,我这里有例子,你瞅瞅:

public class ThreadLoopOne {      public static void main(String[] args) {          Thread t1 = new Thread(new Work(null));         Thread t2 = new Thread(new Work(t1));         Thread t3 = new Thread(new Work(t2));         t1.start();         t2.start();         t3.start();     }     static class Work implements Runnable {          private Thread beforeThread;         public Work(Thread beforeThread){              this.beforeThread = beforeThread;         }         @Override         public void run() {              // 如果有线程,就 join 进来,没有的话就直接输出             if (beforeThread != null ){                  try {                      beforeThread.join();                     System.out.println("thread start : " + Thread.currentThread().getName());                 } catch (InterruptedException e) {                      e.printStackTrace();                 }             }else{                  System.out.println("thread start : " + Thread.currentThread().getName());             }         }     } } 

CountDownLatch

刚才说了,本质就是让线程 B,C 等待线程 A 执行完毕

那么信号量就是一个不错的源码库选择

如果想要实现的话,那大概就是下面这样:

public class ThreadLoopTwo {      public static void main(String[] args) {          // 设置线程 1 的信号量为 0         CountDownLatch cOne = new CountDownLatch(0);         // 设置线程 2 的信号量为 1         CountDownLatch cTwo = new CountDownLatch(1);         // 设置线程 3 的信号量为 1         CountDownLatch cThree = new CountDownLatch(1);         // 因为 cOne 为 0 ,故 t1 可以直接执行         Thread t1 = new Thread(new Work(cOne,cTwo));         // 线程 t1 执行完毕之后,此时的 cTwo 为 0 , t2 开始执行         Thread t2 = new Thread(new Work(cTwo,cThree));         // 线程 t2 执行完毕,此时 cThree 为 0 , t3 开始执行         Thread t3 = new Thread(new Work(cThree,cThree));         t1.start();         t2.start();         t3.start();     }     static class Work implements Runnable{          CountDownLatch cOne;         CountDownLatch cTwo;         public Work(CountDownLatch cOne, CountDownLatch cTwo){              super();             this.cOne = cOne;             this.cTwo = cTwo;         }         @Override         public void run() {              try {                  // 当前一个线程信号量为 0 时,才执行                 cOne.await();                 System.out.println("thread start : " + Thread.currentThread().getName());                 // 后一个线程信号量减 1                 cTwo.countDown();             } catch (InterruptedException e) {                  e.printStackTrace();             }         }     } } 

使用单个线程池

之所以线程 1,2,3 的执行顺序无法保证,是因为在编译器可能会去做一些优化,导致没有办法按照顺序执行

如果我们使用单个线程池去执行的话,那就没有这样的问题了

具体实现:

public class ThreadLoopThree {      public static void main(String[] args) {          Thread t1 = new Thread(new Runnable() {              @Override             public void run() {                  System.out.println("thread start : " + Thread.currentThread().getName() + " run one");             }         });         Thread t2 = new Thread(new Runnable() {              @Override             public void run() {                  System.out.println("thread start : " + Thread.currentThread().getName() + " run two");             }         });         Thread t3 = new Thread(new Runnable() {              @Override             public void run() {                  System.out.println("thread start : " + Thread.currentThread().getName() + " run three");             }         });         ExecutorService executor = Executors.newSingleThreadExecutor();         // 将线程依次加入到线程池中         executor.submit(t1);         executor.submit(t2);         executor.submit(t3);         // 及时将线程池关闭         executor.shutdown();     } } 

CompletableFuture

如果使用 CompletableFuture 来实现的话,亿华云计算代码就非常简洁了

public class ThreadLoopFour {      public static void main(String[] args)  {          Thread t1 = new Thread(new Work());         Thread t2 = new Thread(new Work());         Thread t3 = new Thread(new Work());         CompletableFuture.runAsync(()-> t1.start())                 .thenRun(()->t2.start())                 .thenRun(()->t3.start());     }     static class Work implements Runnable{          @Override         public void run() {              System.out.println("thread start : " + Thread.currentThread().getName());         }     } } 

很赞哦!(6286)