Join a thread pool has multiple threads:
ThreadPool.addThread(t1);
ThreadPool.addThread(t2);
...
ThreadPool.addThread(tn);
Now I want to terminate the m threads practice idea is as follows:
① create a hashMap, the created and the corresponding thread to uniquely identify each thread into it: consoleThreadMap.put (serial, Thread.currentThread ());
② removed after the end of the normal execution threads from hashMap in: consoleThreadMap.remove (serial);
③ In order to remove the threads m needs to be removed in normal operation is performed consoleThreadMap.get (serial) .interrupt ();
Specific examples are as follows:
java.util.Date Import; Import a java.util.concurrent.ConcurrentHashMap; Import org.springframework.stereotype.Service; @Service public class WebConsoleService { Final the shutdown static String = "the shutdown"; public static of ConcurrentHashMap <String, the Thread> = consoleThreadMap new ConcurrentHashMap <String, thread> ( ); // create a hashmap, for storing thread public void WebConsole (Serial String, String cmd, int msgid) { the try { IF (cmd.equalsIgnoreCase ( "the shutdown")!) {// If the termination instruction is not created by the thread pool threads ThreadPoolManager.getInstance () addExecuteTask (new new EachT (cmd, Serial));. } the else { consoleThreadMap.get (serial) .interrupt (); // If termination instruction, is executed interrupt () to terminate a specified threads, particularly where the thread designated by the Serial } } the catch (Exception E) { } } Private class EachT the implements {the Runnable Private String cmd; Private Serial String; public EachT (cmd String, String Serial) { this.cmd = cmd; this.serial Serial =; } @Override public void RUN () { the try { consoleThreadMap.put (Serial, the Thread .currentThread ()); Execute (cmd); } the catch (Exception E) { }finally { consoleThreadMap.remove(serial); //执行完成后,将线程从hashmap中移除 } } public void execute(String cmd) throws InterruptedException { while(!Thread.currentThread().isInterrupted() && true) { System.out.println("ddddddddddddddd"+new Date()); Thread.currentThread().sleep(2000); } } } }
java.util.concurrent.LinkedBlockingQueue Import; Import java.util.concurrent.ThreadPoolExecutor,; Import java.util.concurrent.TimeUnit; Import org.springframework.scheduling.concurrent.CustomizableThreadFactory; public class ThreadPoolManager Final { Private static ThreadPoolManager sThreadPoolManager = new new ThreadPoolManager (); // thread pool to maintain a minimum number of threads Private Final static int SIZE_CORE_POOL = 50; // thread pool to maintain the maximum number of threads Private static int SIZE_MAX_POOL Final = 100; / * * singleton thread pool creation method * / public static the getInstance ThreadPoolManager () { return sThreadPoolManager; } / ************************************************* ************************************************** *********** * several common thread technology ******************************** ************************************************** **************************** * offers four thread pool by the Java Executors, are: * newCachedThreadPool create a thread pool that can be cached, If the required processing exceeds the length of the thread pool, flexible recovered idle thread, if not recovered, the new thread. * NewFixedThreadPool create a fixed-size thread pool, you can control the maximum number of concurrent threads, excess threads will wait in the queue. * NewScheduledThreadPool create a fixed-size thread pool to support regular and periodic task execution. newSingleThreadExecutor * create a single-threaded thread pool, use it only to perform the task only worker threads to ensure that all tasks are performed in a specified order (FIFO, LIFO, priorities). * *. 1, ExecutorService newFixedThreadPool public static (int nThreads) { * New new return the ThreadPoolExecutor (nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, a LinkedBlockingQueue new new <the Runnable> ());} * 2 *, public static ExecutorService newSingleThreadExecutor () { * new new FinalizableDelegatedExecutorService return (new new the ThreadPoolExecutor (. 1,. 1, 0L, TimeUnit.MILLISECONDS, a LinkedBlockingQueue new new <the Runnable> ()));} * *. 3, public static ExecutorService newCachedThreadPool () { return the ThreadPoolExecutor new new * (0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new new SynchronousQueue <the Runnable> ());} ************************************************************ ************************************************** *************************************** / / ** * thread pool * * @param corePoolSize - the number of threads to keep in the pool, including idle threads. * @Param maximumPoolSize - maximum number of threads allowed in the pool. * @Param keepAliveTime - when the number of threads is greater than the core, this is terminated before the excess idle threads waiting for new tasks longest time. * @Param unit - the time unit keepAliveTime parameters. * @Param workQueue - before performing a task for holding queue. This queue Runnable tasks submitted by keeping only the execute method. * @Param handler - because the thread from the scope and capacity of the queue is blocked when the processing program is used. * / // essence newFixedThreadPool create a fixed-size thread pool, you can control the maximum number of concurrent threads, excess threads will be waiting in the queue Private Final ThreadPoolExecutor mThreadPool = new new ThreadPoolExecutor (SIZE_CORE_POOL, SIZE_MAX_POOL, 0L, TimeUnit.MILLISECONDS, new new LinkedBlockingQueue < the Runnable> (), new new CustomizableThreadFactory ( "the Thread--CTSPOOL"), new new ThreadPoolExecutor.AbortPolicy ()); / * * the private constructor access modifiers to prohibit any instantiated. * / Private ThreadPoolManager () { PREPARE (); } * initialize the thread pool, the number of core threads / * * / Private void PREPARE () { IF (mThreadPool.isShutdown () && mThreadPool.prestartCoreThread ()!) { @SuppressWarnings ( "unused") int = StartThread mThreadPool.prestartAllCoreThreads (); } } / * * the thread pool the method of adding the task * / public void addExecuteTask (the Runnable task) { iF (task = null!) { mThreadPool.execute (task); } } / * * is determined whether the last task * / protected Boolean isTaskEnd () { iF ( mThreadPool.getActiveCount () == 0) { return to true; } the else { return to false; } } / * * Get the buffer size * / public int getQueue () { return mThreadPool.getQueue () size ();. } / * * Get the number of threads in the thread pool * / public int getPoolSize () { return mThreadPool.getPoolSize (); } / * * get the number of tasks completed * / public Long getCompletedTaskCount () { return mThreadPool.getCompletedTaskCount (); } / * * off the thread pool is not accepting new tasks will have been accepted play task execution * / public void the shutdown () { mThreadPool.shutdownNow (); } }