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