Reprint please indicate the source
http://blog.csdn.net/pony_maggie/article/details/43897971
Author: Pony
Let's talk about the use of joins, in some cases, if there is a large number of time-consuming operations to be performed in a child thread, the main thread may end before the child thread completes, but if the main thread needs to use the result of the child-threading process, the main thread needs to wait for the child thread to execute and then end, and the join will be used. ()。
Look at the code example,
Class Thread1 extends Thread {public Thread1 () {Super ("[Thread1] thread");
public void Run () {String threadname = Thread.CurrentThread (). GetName ();
System.out.println (ThreadName + "start.");
try {for (int i = 0; i < 5; i++) {System.out.println (ThreadName + "loop at" + i);
Thread.Sleep (1000);
} System.out.println (ThreadName + "end.");
catch (Exception e) {System.out.println ("Exception from" + ThreadName + ". Run");
Class Thread2 extends Thread {Thread1 T1;
Public Thread2 (Thread1 thread1) {super ("[Thread2] Thread");
This.t1 = Thread1;
public void Run () {String threadname = Thread.CurrentThread (). GetName ();
System.out.println (ThreadName + "start.");
try {t1.join ();
System.out.println (ThreadName + "end.");
catch (Exception e) {System.out.println ("Exception from" + ThreadName + ". Run");
}} public class Joinandyieldtestdemo {/** * @param args * * public static void Main (string[] args) {String threadname = Thread.CurrentThread (). GetName ();
System.out.println (ThreadName + "start.");
Thread1 T1 = new Thread1 ();
Thread2 t2 = new Thread2 (t1);
try {t1.start ();
Thread.Sleep (2000);
T2.start ();
T2.join ();
catch (Exception e) {System.out.println ("Exception from Main");
} System.out.println (ThreadName + "end.");
}
}
Run Result:
Main start. Main thread Start
[Thread1] thread start.
[Thread1] Thread loop at 0
[Thread1] thread loop at 1
[Thread2] thread start.
[Thread1] Thread loop at 2
[Thread1] thread loop at 3
[Thread1] thread loop at 4
[Thread1] thread end.
[Thread2] Thread end.
Main end!
From the analysis of the running result, T2 must be finished after T1, the main thread must end after T2, thanks to the join method called.
To say yield, it is recommended to take a look at the previous article, about thread priority,
http://blog.csdn.net/pony_maggie/article/details/43889225
In theory, virtual machines and operating systems will allow higher priority threads to get more execution opportunities, and this should not be confused with time slices. Yield means to let go, give up, and a thread to call yield mean it tells the virtual machine that it is willing to let other threads occupy its place. This is just a hint and does not guarantee an effect (make the current thread go to a running state, runnable).
Look at the code example,
Class Producer extends Thread
{public
void run ()
{for
(int i = 0; i < 5; i++)
{
System.out.pri Ntln ("I am producer:producerd item" + i);
Thread.yield ();
}} Class Consumer extends Thread
{public
void run ()
{for
(int i = 0; i < 5; i++)
{
SYSTEM.O Ut.println ("I am consumer:consumed item" + i);
Thread.yield ();
}} public class Joinandyieldtestdemo
{
/**
* @param args
*
/public static void Main (string[) args)
{
Thread producer = new producer ();
Thread consumer = new Consumer ();
Producer.setpriority (thread.min_priority);
Consumer.setpriority (thread.max_priority);
Producer.start ();
Consumer.start ();
}
The program creates two threads called producers and consumers, the least priority, the latter the highest priority. Calling the yield method, two threads will print sequentially, then give the execution opportunity to each other, and continue to do so. If you do not add yield, the result is to print one first, then another.
Yield's role is clear, set to different priority, and also proves that yield is not affected by the priority size.
The
footage comes from importnew