1, create a thread pool
1, first create a class and then implement Runnable
public class ExectorTest implements Runnable {}
2, first declare a global variable thread pool
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; }
3, and then run () method, create an instance of the thread pool, thread pool is created when the situation remember to create a daemon thread, so you can prevent the service stops, the thread has not stopped the service is still in the process of the tomcat appears .
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; @Override public void RUN () { // create a thread pool, set to daemon, and the main thread can be closed with the this .executorPool = Executors.newFixedThreadPool ( the this .numThreads, new new ThreadFactory () { @Override public Thread newThread(Runnable r) { Thread thread = Executors.defaultThreadFactory().newThread(r); thread.setDaemon(true); return thread; } }); } }
4, then we need to loop method to deal with the number of the calling thread pool in the loop method
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; @Override public void RUN () { // create a thread pool, set to daemon, and the main thread can be closed with the this .executorPool = Executors.newFixedThreadPool ( the this .numThreads, new new ThreadFactory () { @Override public Thread newThread(Runnable r) { Thread thread = Executors.defaultThreadFactory().newThread(r); thread.setDaemon(true); return thread; } }); //循环处理的方法 List<String> handleList = new ArrayList<String>(); for (String handler:handleList) { this.executorPool.submit(new Handler()); } } }
5, the method for processing the paste
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; @Override public void RUN () { // create a thread pool, set to daemon, and the main thread can be closed with the this .executorPool = Executors.newFixedThreadPool ( the this .numThreads, new new ThreadFactory () { @Override public Thread newThread(Runnable r) { Thread thread = Executors.defaultThreadFactory().newThread(r); thread.setDaemon(true); return thread; } }); //循环处理的方法 List<String> handleList = new ArrayList<String>(); for (String handler:handleList) { this.executorPool.submit(new Handler()); } } /** * Data processing threads */ public static class Handler implements Runnable { public Handler() {} @Override public void RUN () { // method for processing data } } }
2, the thread pool is closed
6, the last full-fill method to stop the thread pool, @ PreDestroy method is a method of destruction will be called before the spring of
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; @Override public void RUN () { // create a thread pool, set to daemon, and the main thread can be closed with the this .executorPool = Executors.newFixedThreadPool ( the this .numThreads, new new ThreadFactory () { @Override public Thread newThread(Runnable r) { Thread thread = Executors.defaultThreadFactory().newThread(r); thread.setDaemon(true); return thread; } }); //循环处理的方法 List<String> handleList = new ArrayList<String>(); for (String handler:handleList) { this.executorPool.submit(new Handler()); } } /** * Data processing threads */ public static class Handler implements Runnable { public Handler() {} @Override public void RUN () { // method for processing data } } @PreDestroy public void the shutdown () { // perform a shutdown thread pool, the thread will wait for the completion of IF ( the this .executorPool =! null ) { // Close thread pool the this .executorPool.shutdown (); // wait for completion off, wait five seconds the try { IF (! The this .executorPool.awaitTermination (. 5 , TimeUnit.SECONDS)) { log.info("Timed out waiting for consumer threads to shut down, exiting uncleanly!!"); } } catch (InterruptedException e) { log.info("Interrupted during shutdown, exiting uncleanly!!"); } } } }