Java Thread Execution sequence summary and thread pool termination decision __ thread pool

Source: Internet
Author: User
 
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.



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.