java的線程池的使用

一、線程池的建立

  一、首先建立一個類,而後實現Runnable接口spring

public class ExectorTest implements Runnable {}

  二、首先聲明一個線程池的全局變量tomcat

public class ExectorTest implements Runnable {
        //線程池
        private ExecutorService executorPool;
}

  三、而後在run()方法中,建立線程池實例,建立線程池的時候,切記建立守護線程,這樣能夠防止你的服務中止後,服務的線程還沒中止,就是tomcat的進程還在的狀況出現ide

public class ExectorTest implements Runnable {
        //線程池
        private ExecutorService executorPool;

        @Override
        public void run() {
                //  建立線程池,設置爲守護進程,能夠和主線程一塊兒關閉
               this.executorPool = Executors.newFixedThreadPool(this.numThreads, new ThreadFactory() {
               @Override
               public Thread newThread(Runnable r) {
                    Thread thread = Executors.defaultThreadFactory().newThread(r);
                    thread.setDaemon(true);
                    return thread;
               }
           });  
        }
}

  四、而後咱們須要循環須要處理的方法個數,在循環中調用線程池的方法this

public class ExectorTest implements Runnable {
        //線程池
        private ExecutorService executorPool;

        @Override
        public void run() {
                //  建立線程池,設置爲守護進程,能夠和主線程一塊兒關閉
               this.executorPool = Executors.newFixedThreadPool(this.numThreads, 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());
               }  
        }
}

  五、將處理的方法貼上spa

public class ExectorTest implements Runnable {
        //線程池
        private ExecutorService executorPool;

        @Override
        public void run() {
                //  建立線程池,設置爲守護進程,能夠和主線程一塊兒關閉
               this.executorPool = Executors.newFixedThreadPool(this.numThreads, 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());
               }  
        }

         /**
           * 數據處理線程
           */
        public static class Handler implements Runnable {

              public Handler() {}

              @Override
              public void run() {
                  //處理數據的方法
              }
        }
}

二、線程池的關閉

  六、最後補全中止線程池的方法,@PreDestroy方法是在spring銷燬以前會調用的方法線程

public class ExectorTest implements Runnable {
        //線程池
        private ExecutorService executorPool;

        @Override
        public void run() {
                //  建立線程池,設置爲守護進程,能夠和主線程一塊兒關閉
               this.executorPool = Executors.newFixedThreadPool(this.numThreads, 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());
               }  
        }

         /**
           * 數據處理線程
           */
        public static class Handler implements Runnable {

              public Handler() {}

              @Override
              public void run() {
                  //處理數據的方法
              }
        }

        @PreDestroy
        public void shutdown() {
            // 關閉線程池,會等待線程的執行完成
            if (this.executorPool != null) {
                //  關閉線程池
                this.executorPool.shutdown();

                //  等待關閉完成, 等待五秒
                try {
                    if (!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!!");
                }
            }

        }  
}
相關文章
相關標籤/搜索