java的线程池的使用

2019-08-16 09:49:32来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

java的线程池的使用

1、线程池的创建

  1、首先创建一个类,然后实现Runnable接口

public class ExectorTest implements Runnable {}

  2、首先声明一个线程池的全局变量

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

  3、然后在run()方法中,创建线程池实例,创建线程池的时候,切记创建守护线程,这样可以防止你的服务停止后,服务的线程还没停止,就是tomcat的进程还在的情况出现

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;
               }
           });  
        }
}

  4、然后我们需要循环需要处理的方法个数,在循环中调用线程池的方法

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());
               }  
        }
}

  5、将处理的方法贴上

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() {
                  //处理数据的方法
              }
        }
}

2、线程池的关闭

  6、最后补全停止线程池的方法,@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!!");
                }
            }

        }  
}

 


原文链接:https://www.cnblogs.com/jknote/p/11139862.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Plugin execution not covered by lifecycle configuration: org

下一篇:微服务网关 Spring Cloud Gateway