Java multi-thread join Method Instance code, java multi-thread join instance

Source: Internet
Author: User

Java multi-thread join Method Instance code, java multi-thread join instance

This article focuses on the use of join methods in Java multithreading. The following is an example.

The non-static join () method of Thread allows A Thread B to "join" to the end of another Thread. Before Execution of A, B cannot work. For example:

Thread t = new MyThread ();
T. start ();
T. join ();

In addition,join()The method also contains the overloaded version with the timeout limit. For examplet.join(5000);The thread is waiting for 5000 milliseconds. If the time exceeds this time, the thread stops waiting and becomes runable.

Thread Additionjoin()The result of the thread stack is that the thread Stack has changed. Of course, these changes are instantaneous.

Public class TestJoin {public static void main (String [] args) {MyThread2 t1 = new MyThread2 ("TestJoin"); t1.start (); try {t1.join (); // join () Merge thread. After the child thread is run, the main thread starts executing} catch (InterruptedException e) {}for (int I = 0; I <10; I ++) System. out. println ("I am Main Thread") ;}} class MyThread2 extends Thread {MyThread2 (String s) {super (s) ;} public void run () {for (int I = 1; I <= 10; I ++) {System. out. println ("I am" + getName (); try {sleep (1000); // pause, output once every second} catch (InterruptedException e) {return ;}}}}

Program running result:

I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread

Summary

The above is all about the Java multi-threaded join Method Instance code in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.