Java thread Pool Tool class code (implemented using Java official thread pool class Executorservice) __java multithreaded thread pool

Source: Internet
Author: User
Package common.util;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;

Import Java.util.concurrent.Future;
	
	/** Threading Tool class/public class Threadutil {/** maximum number of threads for long time thread pool * * private static final int max_thread = 2;
	
	/** A long time thread timeout, if the thread execution time exceeds this time will automatically release * * private static final int long_timeout_time = 60000;
	
	/** A short time thread timeout, if the thread execution time exceeds this time will automatically release * * private static final int sort_timeout_time = 12000; /** long time thread pool, for the execution of a short time long threads, you need to set the maximum number of threads/private static Executorservice Longtimethreadpool = Executors.newfixedthreadpool
	
	(Max_thread); /** short time thread pool, used for short execution time threads, without setting the maximum number of threads/private static Executorservice Sorttimethreadpool =
	
	
	Executors.newcachedthreadpool (); /** * Creates a thread, puts the thread into a short thread pool, and starts a daemon monitor line blocks until those * * @param runnable: Threads that need to run * * * @throws Exception * * * * * * IC static synchronized void Getsorttimeoutthread (Runnable Runnable) {//Submit a thread to a short thread pool and then get a Future future<string& Gt Future = (future<string>) sorttimethreadpool.submit (runnable);
	Starts a daemon thread that will compute the timeout of new Threadutil (). Monitorthreadtime (Sort_timeout_time,future);
	/** * Creates a thread, puts the thread into a long thread pool, and starts a daemon monitor line blocks until those * * @param runnable: Threads that need to be run * * @throws Exception * * * The public static synchronized void Getlongtimeoutthread (Runnable Runnable) throws exception{//submits the thread to a short thread pool and then gets a futur
			E future<string> Future = (future<string>) longtimethreadpool.submit (runnable);
	Starts a daemon thread that will compute the timeout of new Threadutil (). Monitorthreadtime (Long_timeout_time,future);
	 /** * Creates a thread, puts the thread into a long thread pool, and starts a daemon monitor line blocks until those * * @param runnable: Threads that need to be run * @param timeouttime: Custom Timeout time * * * @throws Exception * */public static synchronized void Getlongtimeoutthread (Runnable runnable,int timeouttime) t Hrows exception{//submits the thread to the short thread pool and then gets a Future future<string> Future = (future<string>) longtimethreadpo
			Ol.submit (runnable);
Starts a daemon thread that will compute the timeout of new Threadutil (). Monitorthreadtime (Timeouttime,future);	/** * Monitor line blocks until those when the daemon, to the timeout time to view the thread state, if you are in execution stop the thread (this section is problematic) * * @param timeouttime: Timeout * @param future: Execution result table Single, which will be used to compute timeout * * * * * * * private void Monitorthreadtime (int timeouttime,future<string> Future) {//execute daemon thread new Thread (New Runnable () {@Override public void run () {/) Wait a period of time, and then call the execution result, and if it is not done, try to end the try {Thread.sle
				EP (Timeouttime);
				catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); }//To determine if the future is complete, and if the timeout is not completed, try to end the thread if (!future.isdone ()) {//false indicates that the thread is not allowed to execute without interruption, true to allow Future.cancel (true
				);
	}}). Start ();
 }
	
}

Daemon Thread Monitoring Timeout code There is a problem, I found the problem later will be updated, it is best to shield off
Related Article

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.