|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 日志 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private Logger log = LogManager.getLogger(MonitorThread.class.getName()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 得到默认唯一实例 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | //      public MyThreadPool getDefaultInstance(){ | 
|---|
|  |  |  | //         if(myPool == null){ | 
|---|
|  |  |  | //            myPool = new MyThreadPool(null, null, null, null, null) ; | 
|---|
|  |  |  | //         } | 
|---|
|  |  |  | //         return myPool ; | 
|---|
|  |  |  | //      } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 得到唯一实例 | 
|---|
|  |  |  | * @param poolName | 
|---|
|  |  |  | * @param maxNum | 
|---|
|  |  |  | * @param minNum | 
|---|
|  |  |  | * @param freeTimeout | 
|---|
|  |  |  | * @param busyTimeout | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | //      public MyThreadPool getInstance( | 
|---|
|  |  |  | //            String poolName , | 
|---|
|  |  |  | //            Integer maxNum , | 
|---|
|  |  |  | //            Integer minNum , | 
|---|
|  |  |  | //            Long freeTimeout , | 
|---|
|  |  |  | //            Long busyTimeout){ | 
|---|
|  |  |  | //         if(myPool == null){ | 
|---|
|  |  |  | //            myPool = new MyThreadPool(poolName, maxNum, minNum, freeTimeout, busyTimeout) ; | 
|---|
|  |  |  | //         } | 
|---|
|  |  |  | //         return myPool ; | 
|---|
|  |  |  | //      } | 
|---|
|  |  |  | private static final Logger log = LogManager.getLogger(MonitorThread.class) ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 线程池构造方法 | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | this.poolName = poolName ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(maxNum == null || maxNum.intValue() <= 0){ | 
|---|
|  |  |  | maxNum = -1 ; | 
|---|
|  |  |  | if(maxNum == null || maxNum.intValue() < 0){ | 
|---|
|  |  |  | maxNum = 65535 ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(minNum == null || minNum.intValue() < 0){ | 
|---|
|  |  |  | minNum = 0 ; | 
|---|
|  |  |  | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | this.busyTimeout = busyTimeout ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.busiThreads = new ArrayList<MyThread>(); | 
|---|
|  |  |  | this.freeThreads = new ArrayList<MyThread>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //最小化线程池 | 
|---|
|  |  |  | for (int i = 0; i < this.minNum ; i++) { | 
|---|
|  |  |  | MyThread t = new MyThread(this); | 
|---|
|  |  |  | t.start(); | 
|---|
|  |  |  | this.freeThreads.add(t); | 
|---|
|  |  |  | this.currNum++; | 
|---|
|  |  |  | if(maxNum != 0){ | 
|---|
|  |  |  | this.busiThreads = new ArrayList<>(); | 
|---|
|  |  |  | this.freeThreads = new ArrayList<>(); | 
|---|
|  |  |  | //最小化线程池 | 
|---|
|  |  |  | for (int i = 0; i < this.minNum ; i++) { | 
|---|
|  |  |  | MyThread t = new MyThread(this); | 
|---|
|  |  |  | t.start(); | 
|---|
|  |  |  | this.freeThreads.add(t); | 
|---|
|  |  |  | this.currNum++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | this.monitorThread = new MonitorThread(this) ; | 
|---|
|  |  |  | this.monitorThread.start() ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.monitorThread = new MonitorThread(this) ; | 
|---|
|  |  |  | this.monitorThread.start() ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void putJob(Job job) throws Exception { | 
|---|
|  |  |  | if(this.busiThreads == null || this.freeThreads == null){ | 
|---|
|  |  |  | throw new Exception("线程池未启动") ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | synchronized(this.synObj) { | 
|---|
|  |  |  | //log.debug("工作任务分配到线程池中。") ; | 
|---|
|  |  |  | MyThread t = null ; | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 线程工作完成,从busiThreads回归freeThreads | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected void freeThread(MyThread t) { | 
|---|
|  |  |  | protected void freeThread(MyThread t) throws Exception { | 
|---|
|  |  |  | if(this.busiThreads == null || this.freeThreads == null){ | 
|---|
|  |  |  | throw new Exception("线程池未启动") ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | synchronized (synObj) { | 
|---|
|  |  |  | busiThreads.remove(t); | 
|---|
|  |  |  | freeThreads.add(t); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param pool | 
|---|
|  |  |  | * @param pool 池 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public MonitorThread(MyThreadPool pool){ | 
|---|
|  |  |  | this.pool = pool ; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }//end synchronized (pool.synObj) | 
|---|
|  |  |  | }catch(Exception e){ | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | }finally{ | 
|---|
|  |  |  | continue ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 设置线程工作对象 | 
|---|
|  |  |  | * @param job | 
|---|
|  |  |  | * @param job 工作 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected void putJob(Job job) throws Exception { | 
|---|
|  |  |  | if(job == null){ | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void free(){ | 
|---|
|  |  |  | //使本线程回归空闲线程池 | 
|---|
|  |  |  | pool.freeThread(this); | 
|---|
|  |  |  | //空闲开始记时 | 
|---|
|  |  |  | this.time = System.currentTimeMillis() ; | 
|---|
|  |  |  | // 没有可做的了 | 
|---|
|  |  |  | this.canJob = false; | 
|---|
|  |  |  | log.debug("线程池(" + this.pool.poolName + ")中的线程回归空闲集合。"); | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | //使本线程回归空闲线程池 | 
|---|
|  |  |  | pool.freeThread(this); | 
|---|
|  |  |  | //空闲开始记时 | 
|---|
|  |  |  | this.time = System.currentTimeMillis() ; | 
|---|
|  |  |  | // 没有可做的了 | 
|---|
|  |  |  | this.canJob = false; | 
|---|
|  |  |  | log.debug("线程池(" + this.pool.poolName + ")中的线程回归空闲集合。"); | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | log.error("线程池(" + pool.poolName + ")的工作线程释放回归时发生异常:\n" + e.getMessage(), e); | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|