Example of the use of the Join method for Java multithreaded programming _java

Source: Internet
Author: User
Tags thread class

The join method for the thread class was used more than once in the example above. I think you may have guessed what the join method is capable of. Yes, the function of the join method is to make the thread that executes asynchronously into synchronous execution. That is, when the start method of the thread instance is invoked, the method returns immediately, and if you need to use a value computed by this thread after calling the Start method, you must use the Join method. If you do not use the Join method, you cannot guarantee that the thread will run out when a statement after the Start method is executed. After the Join method is used, the program does not go down until the thread exits. The following code demonstrates the use of join.

Copy Code code as follows:

Package mythread;

public class Jointhread extends Thread
{
public static int n = 0;

Static synchronized Void Inc ()
{
n++;
}
public void Run ()
{
for (int i = 0; i < i++)
Try
{
Inc. ();
Sleep (3); To make the results more random, delay 3 milliseconds

}
catch (Exception e)
{
}
}
public static void Main (string[] args) throws Exception
{

Thread threads[] = new THREAD[100];
for (int i = 0; i < threads.length i++)//Create 100 threads
Threads[i] = new Jointhread ();
for (int i = 0; i < threads.length i++)//Run the 100 threads just created
Threads[i].start ();
if (Args.length > 0)
for (int i = 0; i < threads.length i++)//100 threads are executed and continue
Threads[i].join ();
System.out.println ("n=" + JOINTHREAD.N);
}
}

100 threads were established in routine 2-8, and each thread increased the static variable n by 10. If you output n after all 100 threads are executed, this n value should be 1000.
1. Test 1
Use the following command to run the above program:

Copy Code code as follows:

Java mythread. Jointhread

The results of the program's operation are as follows:
Copy Code code as follows:

n=442

The results of this operation may differ in different operating environments, but generally n is not equal to 1000. From the results above, it is certain that the 100 threads do not perform all of the n output.

2. Test 2
Run the above code using the following command:
There is a parameter join in the command line above, in fact, you can use any argument on the command line, as long as you have a parameter, you can use join here just to indicate that you want to use the Join method to synchronize the 100 threads.
The results of the program's operation are as follows:

Copy Code code as follows:

n=1000

Running the above command regardless of the running environment will result in the same results: n=1000. This fully illustrates that all 100 threads are definitely done, so n must be equal to 1000.

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.