Home >Java >javaTutorial >How to make threads execute sequentially in java
The following column java introductory program will introduce how to execute threads sequentially in java. I hope it will be helpful to everyone!
We need to complete such an application scenario:
1. Morning; 2. Testers, product managers, and developers come to the company to work one after another; 3. Product managers plan new requirements; 4. Developers develop new required functions; 5. Testers test new functions.
Planning requirements, developing new functions, and testing new functions are in order. We regard thread1 as the product manager, thread2 as the developer, and thread3 as the tester.
Use the thread's join method
join(): It is Theard's method. Its function is that the calling thread needs to wait for the join() thread to complete before it can continue to use it. run below.
Application scenario: The join method can be used when a thread must wait for another thread to complete execution.
The example is as follows:
package com.zhangsf.javabase.thread.order; /** * @author zhangsf * 通过子程序join使线程按顺序执行 */ public class ThreadJoinDemo { public static void main(String[] args) { final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("产品经理规划新需求"); } }); final Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { thread1.join(); System.out.println("开发人员开发新需求功能"); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread thread3 = new Thread(new Runnable() { @Override public void run() { try { thread2.join(); System.out.println("测试人员测试新功能"); } catch (InterruptedException e) { e.printStackTrace(); } } }); System.out.println("早上:"); System.out.println("测试人员来上班了..."); thread3.start(); System.out.println("产品经理来上班了..."); thread1.start(); System.out.println("开发人员来上班了..."); thread2.start(); } }
Running result:
##Use the join method of the main thread
Here, join() is used in the main thread to block the thread.package com.zhangsf.javabase.thread.order; /** * @author zhangsf * 通过主程序join使线程按顺序执行 */ public class ThreadMainJoinDemo { public static void main(String[] args) throws Exception { final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("产品经理正在规划新需求..."); } }); final Thread thread2 = new Thread(new Runnable() { @Override public void run() { System.out.println("开发人员开发新需求功能"); } }); final Thread thread3 = new Thread(new Runnable() {zzzz @Override public void run() { System.out.println("测试人员测试新功能"); } }); System.out.println("早上:"); System.out.println("产品经理来上班了"); System.out.println("测试人员来上班了"); System.out.println("开发人员来上班了"); thread1.start(); //在父进程调用子进程的join()方法后,父进程需要等待子进程运行完再继续运行。 System.out.println("开发人员和测试人员休息会..."); thread1.join(); System.out.println("产品经理新需求规划完成!"); thread2.start(); System.out.println("测试人员休息会..."); thread2.join(); thread3.start(); } }Running results:
The above is the detailed content of How to make threads execute sequentially in java. For more information, please follow other related articles on the PHP Chinese website!