Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Org.junit.Test;
public class MyTest {@Test public void test1 () {//test1: The main thread will not exit until all threads in the threads pool have finished executing.
Executorservice pool = Executors.newcachedthreadpool ();
for (int i = 0; i < i++) {Final int temp = i;
Pool.execute (New Runnable () {@Override public void run () {//TODO auto-generated method stub try {
Thread.Sleep (3000);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
} System.out.println (temp + "finished");
}
});
Pool.shutdown ()//Stop joining a new thread while (!pool.isterminated ()) {//If all threads perform, then pick the loop.
} System.out.println ("Main thread is done");
@Test public void Test2 () {//Test2: This is relatively simple, starting a new thread, then delaying 2s, and the main thread exits. New Thread (New Runnable () {@Override public void run () {//TODO auto-generated method stub System.out.prin
TLN ("New Thread");
} ). Start ();
Delay (2000);
@Test public void Test3 () {//TEST3: This situation is like trouble, if your program child thread is too long, the life cycle is longer than the main thread,//Then the main thread is over, and the child threads naturally end. The desired result in a child thread is not obtained.
Test result: After waiting for 2s, the output delay is over and does not output new thread. Because the main thread is finished.
If you want to keep the child thread running, you need to join the daemon, or block the main thread and wait for the child thread to complete.
New Thread (New Runnable () {@Override public void run () {//TODO auto-generated Method Stub delay (3000);
System.out.println ("New Thread");
}). Start ();
Delay (2000);//Delay function} @Test public void Test4 () {//TEST4: First, let's say the output: 1. Export the delay is over;2 first. Then output new Thread.
At delay time, because the main thread is blocked, it is only possible to wait for the delay to execute before it turns to the new thread delay (2000);//Delay function new Thread (new Runnable () {@Override
public void Run () {//TODO auto-generated the method stub System.out.println ("New Thread");
}). Start ();
private void delay (long ms) {//TODO auto-generated Method stub try {thread.sleep (ms);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
} SYSTEM.OUT.PRINTLN ("Delay is over");
}
}
Summarize:
1. When using ThreadPool, the first shutdown (no longer accept new threads into the thread pool), and then through the dead loop to determine the isterminated (blocking the main thread) to determine whether all threads completed.
2. If the child thread's life cycle is longer than the main thread, you should add a daemon thread to the child thread, or block the main thread and wait for the child thread to complete (this is similar to the ANR in Android). Oh
3. Remember, do not assume that the thread is new,start in any place. Just like the test4 part of the code above. So when you start a thread, don't put it in the wrong place.