java线程顺序执行 确保T1, T2, T3三个线程顺序执行

 String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start...");

        Thread firstThread = new Thread(new ThreadOne());
        Thread secondThread = new Thread(new ThreadTwo());
        Thread thirdThread = new Thread(new ThreadThree());
    
        // 2. thread run in order
        try {
            firstThread.start();
            firstThread.join();  // 让别的线程等待这个线程结束
            secondThread.start();
            secondThread.join();
            thirdThread.start();
            thirdThread.join();
        } catch (Exception ex) {
            System.out.println("thread join exception.");
        }
        System.out.println(threadName + " end.");

你可能感兴趣的:(多线程,Java)