在多线程中有多种方法让线程按特定顺序执行,你可以用线程类的join()方法在一个线程中启动另一个线程,另外一个线程完成该线程继续执行。

public class Test {


    public static void main(String[] args) throws InterruptedException{
        Thread t1 =  new Thread() {
            @Override
            public void run() {
                for (int i = 1; i < 10; i++) {
                    System.out.println(i);

                }
            }
        };
        
        Thread t2 =  new Thread() {
            @Override
            public void run() {
                for(int i = 10; i < 20;i++) {
                    System.out.println(i);
                }
            }
        };

        Thread t3 =  new Thread() {
            @Override
            public void run() {
                for(int i = 20; i < 30; i++) {
                    System.out.println(i);
                }
            }
        };
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        t3.join();
    }
}

  

相关文章:

  • 2022-12-23
  • 2021-08-13
  • 2021-05-31
  • 2021-12-07
  • 2021-08-05
  • 2021-07-08
  • 2022-12-23
  • 2021-04-15
猜你喜欢
  • 2022-12-23
  • 2022-02-09
  • 2021-09-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2022-12-23
相关资源
相似解决方案