Java thread Join method instance _java

Source: Internet
Author: User

This example describes the join method for thread in Java. Share to everyone for your reference. The implementation methods are as follows:

Join
Public final void Join ()
Throws Interruptedexception waits for the thread to terminate.

Thrown:
Interruptedexception-If any thread interrupts the current thread. When the exception is thrown, the interrupt state of the current thread is cleared.

The following example is popular, that is, when a calls the Join method, the process is allocated only if the thread that is on the same is no longer running

Copy Code code as follows:
public class Jointhread {
public static void Main (String [] args) throws exception{
ThreadTest5 t = new ThreadTest5 ();
Thread A = new Thread (t);
Thread B = new Thread (t);
A.start ();
A.join (); Here a invokes the thread join method, where the main function assigns the thread to a, and the thread is freed when a is finished. To the other object.
B.start ();
for (int i = 1;i < 20;i++)
{
System.out.println ("Apple off the tree" + i);
}
System.out.println ("Apple is Gone");
}
}

Class ThreadTest5 implements Runnable
{
public void Run ()
{
for (int i = 1;i < 10;i++)
{
System.out.println (Thread.CurrentThread (). GetName () + "Eat apple" + (i));
}
}
}

The results of the operation are:

Thread-0 eat Apple 1
Thread-0 Eat Apple 2
Thread-0 eat Apple 3
Thread-0 eat Apple 4
Thread-0 eat Apple 5
Thread-0 Eat Apple 6
Thread-0 Eat Apple 7
Thread-0 Eat Apple 8
Thread-0 Eat Apple 9
Apple 1 off the tree
Apple 2 off the tree
Apple 3 off the tree
Apple 4 off the tree
Apple 5 off the tree
Apple 6 off the tree
Thread-1 eat Apple 1
Apple 7 off the tree
Thread-1 Eat Apple 2
Apple 8 off the tree
Thread-1 eat Apple 3
Apple 9 off the tree
Thread-1 eat Apple 4
Apple 10 off the tree
Thread-1 eat Apple 5
Apple 11 off the tree
Thread-1 Eat Apple 6
Thread-1 Eat Apple 7
Thread-1 Eat Apple 8
Thread-1 Eat Apple 9
Apple 12 off the tree
Apple 13 off the tree
Apple 14 off the tree
Apple 15 off the tree
Apple 16 off the tree
Apple 17 off the tree
Apple 18 off the tree
Apple 19 off the tree
The Apple is gone.

Thread-0 is worth the thread where a is located, and after the thread where a is running, the following thread is contested by the main main function and the B process.

I hope this article will help you with your Java programming.

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.