Completely stop running the thread pool Threadpoolexecutor

Source: Internet
Author: User
Tags sleep thread class

recently, system development encountered such a requirement:

This feature takes a long time to run, and if the process fails, it cannot be stopped and must be watched for a long time, unless the server is stopped.

So, I was thinking about how to add a "stop" function to the function.

after constant thinking and testing, find the idea so simple, directly on the code.

Package com.iamzken.test;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.concurrent.ArrayBlockingQueue;
Import Java.util.concurrent.ThreadPoolExecutor;

Import Java.util.concurrent.TimeUnit;
			public class Threadpoolexecutortest {//thread pool private static threadpoolexecutor pool = new Threadpoolexecutor (3, 5, 5,
	Timeunit.seconds, New Arrayblockingqueue<runnable> (Ten), New Threadpoolexecutor.callerrunspolicy ());
	Defines a thread that is equivalent to the parent thread private static thread t;
	Saves the active thread of all currently executing tasks in the thread pool, equivalent to the child thread private static list<thread> activethreads = new arraylist<thread> (5); Depending on the value of parameter B, decide whether to start the thread or stop the thread public static void Test (Boolean b) {if (b) {System.out.println ("start====================
			====================");
			Stop the parent thread, which uses the thread class's brute force Stop Method Stop T.stop (); Traverse and stop all child threads, here use the thread class of violence stop method stop//Here must be the thread also stopped, originally thought to stop the parent thread, the child thread will automatically stop, it turns out that this is wrong, You must stop the child thread while stopping the parent thread to completely stop the entire thread for (int i = 0; i < activethreads.size (); i++) {Thread t = activethreads.Get (i);
				System.out.println (T.getname ());
			T.stop ();
		} System.out.println ("stop=========================================="); } else {//create parent thread t = new Thread () {@Override public void run () {//Create a thread pool to perform two tasks R1 and R2. Both tasks are dead loops Runnable r1 = new Thread () {@Override public void run () {Thread CurrentThread = Threa
							D.currentthread ();
							Activethreads.add (CurrentThread);
							int i = 1;
								while (true) {System.out.println (Currentthread.getname () + "------------>" + (i++));
								try {thread.sleep (1000);
								} catch (Interruptedexception e) {e.printstacktrace ();
					}
							}
						}
					}; Runnable r2 = new Thread () {@Override public void run () {Thread CurrentThread = Thread.CurrentThread (
							);
							Activethreads.add (CurrentThread);
							int i = 1;
								while (true) {System.out.println (Currentthread.getname () + "------------>" + (i++)); Try {thread.sleep (1000);
								} catch (Interruptedexception e) {e.printstacktrace ();
					}
							}
						}
					};
					Executing two tasks R1 and R2 in the thread pool is actually equivalent to opening two child threads in T, while two child threads are maintained by the thread pool Pool.execute (R1);
				Pool.execute (R2);
			};
			};
		Start parent thread T.start ();
		}}//test method public static void main (string[] args) {//Incoming false represents the task to start the execution of the thread test (false);
		try {thread.sleep (5000);
		} catch (Interruptedexception e) {e.printstacktrace ();
	}//After performing the task for 5 seconds, the incoming false represents the thread to stop test (true);
 }

}


Originally thought to stop the parent thread, the child thread will automatically stop, it turns out that this is wrong, you must stop the parent thread while stopping the child thread to completely stop the entire thread.
Another: Stop thread I used the thread class in the violence stop method stop, because other methods are not thorough, if there are other better methods, friends can communicate with me in time.

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.