API:
Join
Join () throws Interruptedexception
-
waits for the thread to terminate.
-
-
-
Thrown:
-
InterruptedException
-Assume that no matter what thread interrupts the current thread. When the exception is thrown, the
interrupt state of the current thread is cleared.
Join
Join (Long Millis) Throws Interruptedexception
-
The maximum time to wait for the thread to terminate is
millis
milliseconds. Timeout is
0
meant to wait all the time.
-
-
-
Number of references:
-
millis
-The wait time in milliseconds.
-
Thrown:
-
InterruptedException
-Assume that no matter what thread interrupts the current thread.
When the exception is thrown, the interrupt state of the current thread is cleared.
Join
Join (Long millis, int Nanos) throws Interruptedexception
-
The maximum time to wait for the thread to terminate is
millis
milliseconds +
nanos
nanoseconds.
-
-
-
Number of references:
-
millis
-The wait time in milliseconds.
-
nanos
-0-999999 additional nanoseconds to wait.
-
Thrown:
-
IllegalArgumentException
-Assuming that the Millis value is negative, the value of Nanos is not in the 0-999999 range.
-
InterruptedException
-Assume that no matter what thread interrupts the current thread.
When the exception is thrown. The interrupt state of the current thread is cleared.
parsing:
Thread.Join (). is used to specify that the current main thread waits for other threads to run after completion. then continue running the code that follows Thread.Join () .
Example 1:
Package Com.example;import Java.util.date;import Java.util.concurrent.timeunit;public class DataSourcesLoader Implements runnable{@Overridepublic void Run () {System.out.printf ("Beginning Data Sources loading:%s\n", New Date ()); c0/>try { TimeUnit.SECONDS.sleep (4); } catch (Interruptedexception e) { e.printstacktrace (); } System.out.printf ("Data sources loading has finished:%s\n", New Date ());} public static void Main (string[] args) {Datasourcesloader dsloader = new Datasourcesloader (); Thread thread1 = new Thread (Dsloader, "Datasourcethread"); Thread1.start (); try { thread1.join (); } catch (Interruptedexception e) { e.printstacktrace (); } System.out.printf ("Main:configuration has been loaded:%s\n", New Date ());}}
Operation Result:
Beginning data sources Loading:fri Nov 14:27:31 CST 2014Data sources loading has FINISHED:FRI Nov 14:27:35 CST 201 4main:configuration has been LOADED:FRI Nov 14:27:35 CST 2014
Assuming that thread1.join () is removed, the results of the run are as follows:
Main:configuration has been Loaded:fri Nov 14:28:33 CST 2014Beginning data sources Loading:fri Nov 14:28:33 CST 2 014Data sources loading has FINISHED:FRI Nov 14:28:37 CST 2014
Pass the result. It can be very obvious that the Scarlet Letter part: "
Go ahead and run the code after Thread.Join ()”
Example 2:
Package Com.example;import Java.util.date;import Java.util.concurrent.timeunit;public class DataSourcesLoader Implements runnable{@Overridepublic void Run () {System.out.printf ("Beginning Data Sources loading:%s\n", New Date ()); c0/>try { TimeUnit.SECONDS.sleep (4); } catch (Interruptedexception e) { e.printstacktrace (); } System.out.printf ("Data sources loading has finished:%s\n", New Date ());} public static void Main (string[] args) {Datasourcesloader dsloader = new Datasourcesloader (); Thread thread1 = new Thread (Dsloader, "Datasourcethread"); Thread1.start (); try { thread1.join; } catch (Interruptedexception e) { e.printstacktrace (); } System.out.printf ("Main:configuration has been loaded:%s\n", New Date ());}}
This is used in:
Thread1.join (3000);
This means that the main thread will continue to run the code after Thread.Join () only if one of the following 2 conditions is met:
Condition 1:thread1 run complete.
Condition 2: Already waiting for the thread1 to run 3000ms.
Examples. The operating time of the thread1 itself is 4s. The set wait time is 3s, so the resulting run results such as the following. Thread1 is not finished running, the main thread starts running the code behind it. Because the time that Thread1 waits has timed out:
Beginning data sources Loading:fri Nov 14:35:45 CST 2014main:configuration has been Loaded:fri Nov 14:35:48 CST 2 014Data sources loading has FINISHED:FRI Nov 14:35:49 CST 2014
So combine the 2 examples above. We were able to determine the result of the following code:
Example 3:
Package Com.example;import Java.util.date;import Java.util.concurrent.timeunit;public class DataSourcesLoader Implements runnable{@Overridepublic void Run () {System.out.printf ("Beginning Data Sources loading:%s\n", New Date ()); c0/>try { TimeUnit.SECONDS.sleep (4); } catch (Interruptedexception e) { e.printstacktrace (); } System.out.printf ("Data sources loading has finished:%s\n", New Date ());}}
Package Com.example;import Java.util.date;import Java.util.concurrent.timeunit;public class Networkconnectionsloader implements runnable{@Overridepublic void Run () {System.out.printf ("Beginning Network Connect Loading:%s\n ", New Date ()); try {TimeUnit.SECONDS.sleep (6); } catch (Interruptedexception e) {e.printstacktrace (); } System.out.printf ("Network Connect loading has finished:%s\n", New Date ());} public static void Main (string[] args) {Datasourcesloader dsloader = new Datasourcesloader (); Thread thread1 = new Thread (Dsloader, "Datasourcethread"); Networkconnectionsloader Ncloader = new Networkconnectionsloader (); Thread thread2 = new Thread (Ncloader, "Networkconnectionloader"); Thread1.start (); Thread2.start (); try {thread1.join (); Thread2.join (1900); } catch (Interruptedexception e) {e.printstacktrace (); } System.out.printf ("Main:configuration has been loaded:%s\n", New Date ());}}
Operation Result:
Beginning data sources Loading:fri Nov 14:39:20 CST 2014Beginning network Connect LOADING:FRI Nov 14:39:20 CST 201 4Data sources loading has finished:fri Nov 14:39:24 CST 2014main:configuration have been Loaded:fri Nov 14:39:26 C ST 2014Network Connect loading has FINISHED:FRI Nov 14:39:26 CST 2014
Note: Assume that the Thread2.join (1900) Part of Example 3 is changed to:
Thread2.join (3000);
Will the result be the same as above?
In the light of what I started to say, "
Thread.Join (). is used to specify the code after the current main thread waits for other threads to run to finish before continuing to run Thread.Join () . ”
We can see that the results of the operation are different:
Beginning data sources Loading:fri Nov 14:41:21 CST 2014Beginning network Connect LOADING:FRI Nov 14:41:21 CST 201 4Data sources loading has finished:fri Nov 14:41:25 CST 2014Network connect loading have FINISHED:FRI Nov 14 14:41:27 CST 2014main:configuration has been Loaded:fri Nov 14:41:27 CST 2014</span>
As for why there is this difference, I have already explained, I think this should not be difficult to understand.
PS: Part of the code is intercepted from Java 7 Concurrency Cookbook
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Thread.Join () Analysis method