Home > Java > javaTutorial > body text

Introduction to the use of join method in java thread

黄舟
Release: 2016-12-19 14:39:44
Original
1526 people have browsed it

In the above example, the join method of the Thread class is used many times. I think you may have guessed what the function of the join method is. Yes, the function of the join method is to turn the asynchronous execution thread into a synchronous execution. That is to say, when the start method of the thread instance is called, this method will return immediately. If you need to use a value calculated by this thread after calling the start method, you must use the join method. If you do not use the join method, there is no guarantee that when a statement following the start method is executed, the thread will be executed. After using the join method, the program will not continue execution until this thread exits.
The following code demonstrates the use of join.

package mythread;

 public class JoinThread extends Thread
 {
     public static volatile int n = 0;

     public void run()
     {
         for (int i = 0; i < 10; i++, n++)
             try
             {
                 sleep(3);  // 为了使运行结果更随机,延迟3毫秒
             }
             catch (Exception e)
             {
             }                                      
     }
     public static void main(String[] args) throws Exception
     {
         Thread threads[] = new Thread[100];
         for (int i = 0; i < threads.length; i++)  // 建立100个线程
             threads[i] = new JoinThread();
         for (int i = 0; i < threads.length; i++)   // 运行刚才建立的100个线程
             threads[i].start();
         if (args.length > 0)  
             for (int i = 0; i < threads.length; i++)   // 100个线程都执行完后继续
                 threads[i].join();
         System.out.println("n=" + JoinThread.n);
     }
 }
Copy after login

In routine 2-8, 100 threads are created, and each thread increases the static variable n by 10. If n is output after all 100 threads are executed, the n value should be 1000.


1. Test 1

Use the following command to run the above program:

1 java mythread.JoinThread
Copy after login

The running results of the program are as follows:

1 n=442
Copy after login


This running result may have some differences in different operating environments, but generally n will not be equal to 1000. From the above results, we can be sure that n is not output after all 100 threads have been executed.


2. Test 2

Use the following command to run the above code:

There is a parameter join in the above command line. In fact, any parameter can be used in the command line, as long as there is one parameter, Join is used here just to indicate that the join method is used to execute these 100 threads synchronously.

The running result of the program is as follows:

1 n=1000
Copy after login

No matter what operating environment you run the above command, you will get the same result: n=1000. This fully demonstrates that all 100 threads must have been executed. Therefore, n will definitely equal 1000.

The above is an introduction to the use of the join method of java threads. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!