Waiting for the thread to terminate

Source: Internet
Author: User

In some cases, we have to wait for the thread to terminate. For example, our program must initialize some of the necessary resources before performing other tasks. You can use threads to complete these initialization tasks, wait for the thread to terminate, and then perform other tasks for the program.

For this purpose, we use the Join () method of the thread class. When a thread object's join () method is called, the thread that invokes it is suspended until the thread object finishes its task.

package concurrency;import java.util.date;import java.util.concurrent.timeunit;public  Class datasourcesloader implements runnable {    private int  second;    public datasourcesloader (Int second) {         this.second = second;    }    @ Override    public void run ()  {         system.out.printf ("beginning data sources loading: %s\n",  new Date ());         try {             timeunit.seconds.sleep (second);        }  catch  (interruptedexception e)  {             e.printstacktrace ();  &Nbsp;      }        system.out.printf (" Data sources loading has finished:  %s\n ",  new date ());     }    public static void main (String[] args)  {         DataSourcesLoader dsLoader = new  Datasourcesloader (4);        thread thread1 = new  Thread (Dsloader, "DataSourceThread1");        datasourcesloader  Dsloader2 = new datasourcesloader (6);         thread  thread2 = new thread (DsLoader2, "DataSourceThread2");         thread1.start ();         thread2.start ();         try  {            thread1.join ();             thread2.join ();         } catch  (interruptedexception e)  {             e.printstacktrace ();        }         system.out.printf ("main: configuration has been  Loaded: %s\n ",  new date ());     }}

When the THREAD1 thread finishes running and the thread2 thread ends, the main thread object will continue to run and print out the final information.

Java provides two different forms of join () methods:

    • Join (long milliseconds)

    • Join (Long Millseconds,long Nanos)

When a thread calls the join () method of another thread, if it is using the first join (), it does not have to wait for the called thread to terminate, and it will continue to run if the millisecond clock specified by the parameter has arrived. For example, with this code threadb.join (Threada), Threada will hang until one of the following two conditions is met:

    • The THREADB has been run to completion.

    • The clock has been in the past 1000 milliseconds.

When any one of the two conditions is true, the join () method returns. The second join () method is similar to the first, except that it accepts two parameters of milliseconds and nanoseconds.

Waiting for the thread to terminate

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.