Sequential thread execution in Java

Source: Internet
Author: User

What should I do if I want to run threadone, threadtwo, and threadthree in the order of threadone-> threadtwo-> threadthree? Here we need to use a simple thread method join ().

Description of the join () method: the join method suspends the current call thread until the call thread finishes executing (join () method suspends the execution of the calling thread until the object called finishes its execution ).

The following is an example:

/** * Thread one for test. */public class ThreadOne implements Runnable {    @Override    public void run() {        String threadName = Thread.currentThread().getName();        System.out.println(threadName + " start...");        System.out.println(threadName + " end.");    }}
/** * Thread two for test. */public class ThreadTwo implements Runnable {    @Override    public void run() {        String threadName = Thread.currentThread().getName();        System.out.println(threadName + " start...");        System.out.println(threadName + " end.");    }}
/** * Thread three for test. */public class ThreadThree implements Runnable {    @Override    public void run() {        String threadName = Thread.currentThread().getName();        System.out.println(threadName + " start...");        System.out.println(threadName + " end.");    }}
public class JoinMainTest {    public static void main(String[] args) {        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());        // 1. no order thread run        firstThread.start();        secondThread.start();        thirdThread.start();        System.out.println(threadName + " end.");    }}

The above result is as follows:

/**
*
Main start...
Thread-0 start...
Main end.
Thread-0 end.
Thread-1 start...
Thread-1 end.
Thread-2 start...
Thread-2 end.
*/

public class JoinMainTest {    public static void main(String[] args) {        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.");    }}

The result is executed in sequence:

/**
*
Main start...
Thread-0 start...
Thread-0 end.
Thread-1 start...
Thread-1 end.
Thread-2 start...
Thread-2 end.
Main end.
*/

Let's take a look at the source code of join (). join () calls join (0). We can see that join is actually wait until the thread execution is complete.

public final synchronized void join(long millis) throws InterruptedException {    long base = System.currentTimeMillis();    long now = 0;    if (millis < 0) {        throw new IllegalArgumentException("timeout value is negative");    }    if (millis == 0) {        while (isAlive()) {            wait(0);        }    } else {        while (isAlive()) {            long delay = millis - now;            if (delay <= 0) {                break;            }            wait(delay);            now = System.currentTimeMillis() - base;        }    }
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.