Home > Java > javaTutorial > How to make threads execute sequentially in java

How to make threads execute sequentially in java

王林
Release: 2019-11-25 14:01:06
forward
2855 people have browsed it

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();
    }
}
Copy after login

Running result:

How to make threads execute sequentially in java

##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();
    }
}
Copy after login
Running results:


How to make threads execute sequentially in java

#There are many ways to execute threads sequentially, and I will introduce them one by one in the future.

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template