当前位置: 移动技术网 > IT编程>开发语言>Java > java的线程池的使用

java的线程池的使用

2019年07月06日  | 移动技术网IT编程  | 我要评论

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

        }  
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网