Java Thread Join__java

Source: Internet
Author: User


thread provides a way to--join a thread to wait for another thread to complete. When a join method of another thread is invoked in a program execution stream, the calling thread is blocked, knowing that the join thread joined by the Join method is finished executing.


Join methods are usually called by programs that use threads to divide large problems into small problems, each of which assigns a thread. When all the minor problems are processed, the main thread is invoked to further operate.


Package com.hehe.thread;

public class Join extends thread{public
	
	join (String name) {
		super (name);
	}
	
	Override the Run method to define the thread executor public
	void run () {for
		(int i = 0; i < i++) {
			System.out.println (getName () + "" + i) ;
		}
	}
	
	public static void Main (string[] args) throws Exception {
		//boot child thread new
		Join ("New Thread"). Start ();
		
		for (int i = 0; i < i++) {
			if (i==5) {
				join jointhread=new join ("thread being join");
				Jointhread.start ();
				
				The main thread invokes the Join method on the Jointhread field, and the main thread must wait until Jointhread execution finishes before executing the
				jointhread.join ();
			System.out.println (Thread.CurrentThread (). GetName () + "" + i);}}}




There are 3 threads in the above program, and the main method starts with a child thread called "new thread" that will execute concurrently with the Mian thread. When the main thread's loop variable I equals 5 o'clock, the thread named "Join Thread" is started, the threads are not executed concurrently with the main threads, and the main thread must wait for the thread to finish execution before it can be executed down. When a thread named "Threaded by join" executes, only 2 child threads are actually executing concurrently, while the main thread is in a waiting state.


Three overloaded forms of the Join method:

1.join (): Waiting for a join thread to execute.

2.join (Long Millis): The maximum time for a thread waiting to be join is millis milliseconds. If the thread that was join in Millis milliseconds has not finished executing, it is no longer waiting.

3.join (Long Millis,int Nanos): The maximum time for a thread waiting to be join is Millis millisecond plus Nanos nanosecond.









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.