From ca2417166fe917aeb45d08df6ff152f9c7a0060a Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期日, 27 四月 2025 10:57:17 +0800 Subject: [PATCH] 修改拦载器 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPoolImp.java | 439 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 439 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPoolImp.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPoolImp.java new file mode 100644 index 0000000..ba8154a --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPoolImp.java @@ -0,0 +1,439 @@ +package com.dy.common.threadPool; + +import java.util.*; + +import org.apache.logging.log4j.*; + +import com.dy.common.threadPool.ThreadPool.Job; + +public class ThreadPoolImp { + + /** + * 绾跨▼姹犲疄鐜� + * @author Administrator + * + */ + public class MyThreadPool implements ThreadPool.Pool{ + + /** + * 绾跨▼姹犲敮涓�瀹炰緥 + */ + public MyThreadPool myPool ; + + /** + * 绾跨▼姹犲悕绉帮紱 + */ + private String poolName ; + /** + * 绌洪棽绾跨▼闆嗗悎 + */ + private List<MyThread> freeThreads; + + /** + * 宸ヤ綔涓殑绾跨▼闆嗗悎 + */ + private List<MyThread> busiThreads; + + /** + * 绾跨▼姹犳渶澶х嚎绋嬫暟 , 鑻axNum <= 0锛屽垯maxNum = 1 銆� + */ + private int maxNum; + + /** + * 鏈�灏忕嚎绋嬫暟锛岃嫢minNum > maxNum锛屽垯minNum = maxNum 銆� + */ + private int minNum; + + /** + * 褰撳墠绾跨▼姹犱腑鐨勭嚎绋嬫暟銆� + */ + private int currNum; + + /** + * 绌洪棽绾跨▼瓒呮椂鐨勬椂闀�(绉�)锛岃秴杩囪繖涓椂闂达紝灏辨竻闄ゅ浣欑嚎绋嬶紝 + * 浣跨┖闂茬嚎绋嬫渶澶氭槸minNum涓� + */ + private long freeTimeout ; + + /** + * 蹇欑绾跨▼瓒呮椂鐨勬椂闀�(绉�)锛岃秴杩囪繖涓椂闂达紝灏辫涓烘槸宕╂簝绾跨▼锛屽皢琚竻闄ゃ�� + */ + private long busyTimeout ; + + /** + * 鍚屾瀵硅薄 + */ + private Object synObj = new Object(); + + /** + * 鍐呴儴鍚姩鐨勭┖闂插拰蹇欑瓒呮椂绾跨▼鐨勭嚎绋� + */ + private Thread monitorThread ; + + /** + * 鏃ュ織 + */ + private static final Logger log = LogManager.getLogger(MonitorThread.class) ; + + /** + * 绾跨▼姹犳瀯閫犳柟娉� + * @param poolName 绾跨▼姹犲拰绾跨▼鍚嶇О + * @param maxNum 绾跨▼姹犳渶澶х嚎绋嬫暟锛岃嫢涓�-1锛屼笉鍙楅檺鍒� + * @param minNum 绾跨▼姹犳渶灏忕嚎绋嬫暟锛屾垨鍒濆绾跨▼鏁� + * @param freeTimeout 绌洪棽绾跨▼瓒呮椂鏃堕暱(绉�) + * @param busyTimeout 蹇欑绾跨▼瓒呮椂鏃堕暱(绉�)锛岃嫢涓�-1锛屼笉鍙楅檺鍒� + */ + public MyThreadPool( + String poolName , + Integer maxNum , + Integer minNum , + Long freeTimeout , + Long busyTimeout) { + if(poolName == null){ + poolName="绾跨▼姹�" ; + } + this.poolName = poolName ; + + if(maxNum == null || maxNum.intValue() < 0){ + maxNum = 65535 ; + } + if(minNum == null || minNum.intValue() < 0){ + minNum = 0 ; + } + if(minNum > maxNum){ + minNum = maxNum ; + } + this.maxNum = maxNum ; + this.minNum = minNum ; + this.currNum = 0; + + if(freeTimeout == null || freeTimeout.longValue() <= 0){ + freeTimeout = 60000L ; + } + this.freeTimeout = freeTimeout ; + + if(busyTimeout == null || busyTimeout.longValue() <= 0){ + this.busyTimeout = -1L ; + }else{ + this.busyTimeout = busyTimeout ; + } + 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() ; + } + } + /** + * 绾跨▼姹犱腑绾跨▼涓暟 + * @return + */ + @Override + public Integer size() { + return currNum ; + } + @Override + public Integer maxThread() { + return maxNum ; + } + @Override + public Integer minThread() { + return minNum ; + } + + /** + * 鎶婃墍瑕佹墽琛岀殑宸ヤ綔瀵硅薄瀹炰緥鏀惧叆绾跨▼姹犱腑 + * @param job ThreadJob 宸ヤ綔瀵硅薄瀹炰緥 + * @throws Exception + */ + @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 ; + if (this.freeThreads.size() == 0) { + //褰撳墠娌℃湁绌洪棽绾跨▼ + if (this.maxNum == -1 || this.currNum < this.maxNum) { + //褰撳墠绾跨▼鏁版湭杈惧埌鏈�澶у�� , 澧炲姞鏂扮殑绾跨▼ + t = new MyThread(this); + t.start(); + this.currNum++; + } else { + //褰撳墠绾跨▼杈惧埌鏈�澶ф暟浜嗭紝绛夊緟鍥炲綊绾跨▼ + while (freeThreads.size() == 0) { + //濡傛灉娌℃湁绌洪棽鐨勭嚎绋�,绛夊緟宸ヤ綔绾跨▼宸ヤ綔瀹屾垚鍥炴潵 + try { + //log.warn("绾跨▼姹�(" + this.poolName + ")涓嚎绋嬫暟杈惧埌涓婇檺锛屾柊宸ヤ綔浠诲姟绛夊緟閲婃斁绾跨▼!"); + synObj.wait(); + } catch (Exception e) { + log.error("'" + this.poolName + "'绾跨▼姹犱腑绾跨▼绛夊緟閲婃斁绾挎椂鍙戠敓绛夊緟寮傚父!", e); + } + t = (MyThread) freeThreads.get(0); + if (t != null) { + //璇存槑寰楀埌浜嗛噴鏀惧洖鏉ョ殑绾跨▼ + freeThreads.remove(0); + break; + }else{ + //璇存槑娌℃湁寰楀埌閲婃斁鍥炴潵鐨勭嚎绋嬶紝鍙互鍏朵粬绾跨▼ + continue; + } + }//end while + }//end else + }//end if(freeThreads.size() == 0) + else { + t = (MyThread) freeThreads.get(0); + freeThreads.remove(0); + } + busiThreads.add(t); + t.putJob(job); + } + } + + /** + * 绾跨▼宸ヤ綔瀹屾垚锛屼粠busiThreads鍥炲綊freeThreads + */ + 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); + synObj.notify(); + } + } + + /** + * 鐩戞帶瓒呮椂绾跨▼鐨勭嚎绋� + * @author Administrator + * + */ + protected class MonitorThread extends Thread { + private MyThreadPool pool ; + + private MyThread t ; + private Iterator<?> it ; + + /** + * + * @param pool 姹� + */ + public MonitorThread(MyThreadPool pool){ + this.pool = pool ; + } + /** + * + */ + @SuppressWarnings("finally") + public void run(){ + long interval = pool.freeTimeout ; + if(pool.busyTimeout > 0 && pool.busyTimeout < pool.freeTimeout){ + interval = pool.busyTimeout ; + } + boolean isException = false ; + while(true){ + t = null ; + it = null ; + try{ + MonitorThread.sleep(interval) ; + }catch(Exception e){ + isException = true ; + }finally{ + if(isException){ + isException = false ; + continue ; + } + } + try{ + synchronized (pool.synObj) { + if(pool.freeThreads.size() > pool.minNum){ + //濡傛灉绌洪棽绾跨▼澶т簬鏈�灏忕嚎绋嬫暟锛屽垯娓呯悊绌洪棽绾跨▼ + int num = pool.freeThreads.size() - pool.minNum ; + int count = 0 ; + it = pool.freeThreads.iterator() ; + while(it.hasNext()){ + if(count == num) { + break ; + } + count ++ ; + t = (MyThread)it.next() ; + if((System.currentTimeMillis() - t.time) >= pool.freeTimeout){ + it.remove() ; + pool.currNum-- ; + //log.debug("绾跨▼姹�(" + pool.poolName + ")涓竻闄や簡涓�涓秴鏃剁┖闂茬嚎绋�!"); + t.destroy() ; + t = null ; + } + } + }//end if + + if(pool.busyTimeout != -1 && pool.busyTimeout > 0){ + it = pool.busiThreads.iterator() ; + while(it.hasNext()){ + t = (MyThread)it.next() ; + if((System.currentTimeMillis() - t.time) >= pool.busyTimeout){ + it.remove() ; + pool.currNum-- ; + log.error("绾跨▼姹�(" + pool.poolName + ")涓竻闄や簡涓�涓秴鏃跺穿婧�(蹇欑)绾跨▼!"); + t.destroy() ; + t = null ; + } + } + } + }//end synchronized (pool.synObj) + }catch(Exception e){ + e.printStackTrace(); + }finally{ + continue ; + } + }//end while(true) + } + } + + + } + + /** + * 姹犱腑绾跨▼瀹炵幇 + * @author Administrator + * + */ + public class MyThread extends Thread{ + + /** + * 鏍囩ず绾跨▼鏄椿鐨� + */ + private boolean living = true ; + + /** + * 绾跨▼蹇欑鎴栫┖闂茶鏃跺櫒 + */ + protected long time ; + + /** + * 褰撳墠绾跨▼鎵�澶勭殑绾跨▼姹� + */ + private MyThreadPool pool ; + /** + * 绾跨▼鍏蜂綋宸ヤ綔鐨勫洖璋冪被 + */ + private Job job ; + + /** + * 鎸囩ず绾跨▼鍙互宸ヤ綔 + */ + private Boolean canJob ; + + private Logger log = LogManager.getLogger(MyThread.class.getName()) ; + + protected MyThread(MyThreadPool pool) { + super(); + this.pool = pool ; + this.time = 0 ; + this.canJob = false ; + } + + /** + * 璁剧疆绾跨▼宸ヤ綔瀵硅薄 + * @param job 宸ヤ綔 + */ + protected void putJob(Job job) throws Exception { + if(job == null){ + this.job = new Job(){ + public void execute(){ + } + public void destroy(){ + } + public boolean isDestroy(){ + return false ; + } + }; + } + synchronized (this) { + this.job = job ; + this.canJob = true; + //蹇欑璁版椂寮�濮� + this.time = System.currentTimeMillis() ; + this.notify(); + } + } + + /** + * + */ + @SuppressWarnings("finally") + @Override + public void run(){ + while (living){ + synchronized (this){ + while(!canJob){ + //褰撲笉鑳藉伐浣滄椂 + try{ + this.wait(); + }catch (Exception e) { + log.error("绾跨▼姹�(" + pool.poolName + ")鐨勫伐浣滅嚎绋嬬瓑寰呭彲浠ュ伐浣滅殑淇″彿鏃跺彂鐢熺瓑寰呭紓甯�:\n" + e.getMessage(), e); + this.canJob = false ; + continue; + } + } + /////////////////////// + //琚敜閱掞紝鏈夋柊宸ヤ綔浜� + try{ + if(this.job != null){ + this.job.execute() ; + } + }catch (Exception ee) { + log.error("绾跨▼姹�(" + pool.poolName + ")鐨勫伐浣滅嚎绋嬪湪鎵ц宸ヤ綔鏃跺彂鐢熷紓甯�:\n" + ee.getMessage(), ee); + //ee.printStackTrace() ; + }finally { + this.canJob = false ; + this.job = null ; + if(living){ + //绾跨▼鏈閿�姣� + this.free() ; + } + continue; + } + }// end synchronized(this) + }// end while(living) + } + + public void free(){ + 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(); + } + + } + + /** + * 閿�姣佸伐浣滅嚎绋� + */ + public void destroy() { + //绾跨▼閿�姣佸墠锛岄鍏堟妸鍏舵墍鎸佹湁鐨勫伐浣滈攢姣侊紝鍚﹀垯绾跨▼鍋滀笉涓嬫潵锛屼篃閿�姣佷笉浜� + if(this.job != null){ + this.job.destroy(); + } + this.living = false ;//浣跨嚎绋嬩粠run鏂规硶鐨勯暱涔厀hile(living)涓��鍑烘潵锛屼粠绾跨▼鑷姩閿�姣� + this.job = null ; + } + } +} -- Gitblit v1.8.0