1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.dy.common.threadPool;
 
 
public interface ThreadPool {
    
    /**
     * 线程池
     * @author Administrator
     *
     */
    public interface Pool{
        /**
         * 线程池中线程个数
         * @return
         */
        public Integer size() ;
        /**
         * 线程池中线程最大限制个数
         * @return
         */
        public Integer maxThread() ;
        /**
         * 线程池中线程最小限制个数
         * @return
         */
        public Integer minThread() ;
        /**
         * 把所要执行的工作任务对象实例放入线程池中
         * @param job ThreadJob 工作任务对象实例
         * @throws Exception
         */
        public void putJob(Job job) throws Exception  ;
    }
 
    /**
     * 线程池工作任务
     * @author Administrator
     *
     */
    public interface Job{
        /**
         * 线程池线程执行时的回调方法。
         * 注意:
         * 1、这个方法内,尽量不使用try catch语句,如果确实需要用,
         * 要一定用try catch finnaly语句,而且finnaly内不要再有可能产生异常。
         * 否则线程得不到结束,不能回归空闲线程池。
         * 2、如果在短线程池中,这个方法内不能执行永循环工作,例如有while(true)这样的工作任务,
         * 否则至超时时间,系统强制停止此工作。
         * 3、如果在短线程池中,这个方法内只能执行短时间即可完成的工作,工作完成后即退出本方法体。
         * @throws Exception
         */
        public void execute() throws Exception ;
 
        /**
         * 外部调用,强制销毁工作
         * 主要应用:
         *     如果在execute()方法中有while(true){}死循环,这种情况下,外部调用者没法叫线程停下来的,
         *  如果外部调用者需要停止此工作,Job接口设计了destroy方法,外部调用者用job对象调此方法完成停止工作。
         *  这时需要在while(true){}死循环中判断是否外部调用者要求停止执行job
         *  示例代码:
         *  public void execute(){
         *      while(true){
         *          if(this.isDestroy){
         *              break;
         *          }
         *          ... ...
         *      }
         *  }
         *  public void destroy(){
         *      this.isDestroy = true ;
         *  }
         *
         *  需要while(true){}死循环的工作job一定在长线程池中工作,如果在短线程池中工作,线程池监控线程就会
         *  以忙碌超时原因把线程强制销毁(线程池监控线程调用工作线程的destroy方法),这里的销毁实际上销毁不了
         *  线程,这个线程仍然是活着的, 线程里的job仍然被执行,所以在线程的destroy方法中调用了job.destroy(),
         *  使job停止下来,线程也自然执行完毕而得以停止,以上是代码逻辑存在while(true){}死循环,
         *  如果不是代码逻辑,而是程序bug造成的死循环,或程序抛出了异常且未捕获异常,这时无论如何也停止
         *  不了工作job,也销毁不了这个线程的
         */
        public void destroy() ;
 
        /**
         * 判断,工作是否被外部强制销毁,销毁后,持有本job的线程就能施放回归
         */
        public boolean isDestroy() ;
        
    }
}