[Translation] Java Thread join example and Explanation

Source: Internet
Author: User

Java Thread join example and Explanation

The Java Thread join method is used to pause the current Thread until the end of the join operation. Java has three overloaded join methods:

Public final void join (): This method changes the current thread to wait until the end of the thread that executes the join operation. If the thread is interrupted during execution, InterruptedException is thrown.

Public final synchronized void join (long millis): This method changes the current thread to wait until the thread that executes the join operation ends or waits for millis time after the join operation is executed. Because Thread Scheduling depends on the implementation of the operating system, it cannot guarantee that the current thread will change to RUnnable at millis time.

Public final synchroinzed void join (long millis, int nanos): This method changes the current thread to wait until the thread that executes the join operation ends or waits for millis + nanos time after the join operation.

The following example shows the usage of the Thread join method. In this program, ensure that the main thread is the last completed thread, and ensure that the third thread (third thread) is executed only after the first thread ends.

=  Thread( MyRunnable(), "t1"=  Thread( MyRunnable(), "t2"=  Thread( MyRunnable(), "t3"        2000                "All threads are dead, exiting main thread"

 

 MyRunnable  "Thread started:::"+4000"Thread ended:::"+

 

The program output result is as follows:

Thread started: t1
Thread ended: t1
Thread started: t2
Thread started: t3
Thread ended: t2
Thread ended: t3
All threads are dead, exiting main thread

 

Link: http://www.journaldev.com/1024/java-thread-join-example-with-explanation

Related Article

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.