Waiting for thread to end (join)

Source: Internet
Author: User
Tags joins static class

In many cases, collaboration between threads and human-to-human collaboration is very similar. A very common form of cooperation is the Division of labor. With our very familiar software development as an example, when a project is carried out, there should always be a number of colleagues claiming to be "demand analysts", first to collate and summarize the needs and functions of the system, and then, in writing, to give a description of the requirements or similar reference documents, then, software designers, research and development engineers will swarmed for software development. If you lack the job output of a demand analyst, software development can be more difficult. Therefore, as a software developer, he always likes to wait for the demand analyst to complete the task he should complete before he is willing to join the work. Simply put, developers need to wait for a demand analyst to finish his work before they can do research and development.

This relationship corresponds to a multithreaded application, and many times the input of one thread may be very dependent on the output of another or multiple threads, at which point the thread needs to wait for the dependent thread to execute before continuing, and the JDK provides a join () operation to implement this function. Two join methods are shown below:

Public final void Join () throws Interruptedexceptionpublic final synchronized void join (long Millis) throws Interruptedexc Eption

The first join method represents a wireless wait, and he will block the current thread until the target thread finishes executing. The second method gives a maximum wait time, and if the target thread is still executing over a given time, the current thread will continue to execute because "wait is not enough".

The translation of English joins is usually meant to be added. It's also very relevant here. Because one thread joins another thread, the best way is to wait for it to go together.

A join instance is provided here:

public class joinmain{public    volatile static int i = 0;    public static class Addthread extends thread{public        void Run () {for            (i=0;i<10000000;i++);        }    }    public static void Main (String args[]) throws interruptedexception{        addthread at = new Addthread ();        At.start ();        At.join ();        System.out.println (i);    }}

In the main function, if you do not use join () to wait for Addthread, then the resulting i is likely to be 0 or a very small number. Since the addthread has not yet begun to execute, the value of I has been output. However, after using the join () method, it means that the main thread is willing to wait for the addthread to complete and then go forward along with Addthread, so when the join () returns, the Addthread is executed, so I always 10000000.

About join (). I would also like to add that the essence of join () is to have the calling thread wait () on the current thread object instance. The following is the core code snippet of the join () implementation in the JDK

while (IsAlive ()) {    wait (0);}

As you can see, it allows the calling thread to wait on the current thread object. When the thread finishes executing, the waiting thread calls Notifyall () to notify all waiting threads to continue execution before exiting. Therefore, it is important to note that you should not use methods such as Wait () or notify () on the thread object instance in your application, as this is likely to affect the work of the system API or be affected by the system API.

Waiting for thread to end (join)

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.