From d4fa5269e214e8bbe1f7dd79a922cefe9fc9916b Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期一, 17 七月 2023 14:28:18 +0800
Subject: [PATCH] 新增线程池工具类

---
 src/main/java/com/hx/util/thread/ExecutorServiceTool.java |  131 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 131 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/hx/util/thread/ExecutorServiceTool.java b/src/main/java/com/hx/util/thread/ExecutorServiceTool.java
new file mode 100644
index 0000000..715d81d
--- /dev/null
+++ b/src/main/java/com/hx/util/thread/ExecutorServiceTool.java
@@ -0,0 +1,131 @@
+package com.hx.util.thread;
+
+import lombok.Setter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.LinkedBlockingDeque;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+
+/**
+ * 鍒涘缓绾跨▼姹犵殑锛屼富瑕侀伩鍏嶇嚎绋嬮槦鍒楄秴锛屽鑷存湇鍔″櫒鐖嗕簡
+ */
+public class ExecutorServiceTool {
+
+    /**log4j鏃ュ織*/
+    private static final Logger logger = LoggerFactory.getLogger(ExecutorServiceTool.class.getName());
+
+    /**绾跨▼姹�-鑾峰彇闃熷垪鐨�*/
+    private ThreadPoolExecutor threadPool;
+    /**鏈�澶ч槦鍒�*/
+    private int maxQueue;
+    /**鏈�澶х嚎绋嬫暟*/
+    private int maxTread;
+    /**鍒涘缓闃熷垪*/
+    private ThreadTaskLinkedBlockingDeque<Runnable> queue = new ThreadTaskLinkedBlockingDeque<>();
+
+    public ExecutorServiceTool() {
+    }
+
+    public ExecutorServiceTool(int initTread, int maxQueue) {
+        createThread(initTread,maxQueue);
+    }
+
+    public ExecutorServiceTool(int initTread, int maxTread, int maxQueue) {
+        this.maxTread = maxTread;
+        createThread(initTread,maxTread,maxQueue);
+    }
+
+
+    /**鍒涘缓绾跨▼姹�
+     * @param initTread 鍒濆鍖栫嚎绋嬫睜
+     * @param maxQueue 鏈�澶ч槦鍒楁柊澧為槦鍒楋紝娉ㄦ剰锛氳繖閲屽彧鍋氫紶鍊硷紝涓嶉檺鍒剁湡瀵�
+     *                  绾跨▼姹犻檺鍒堕槦鍒楋紝鍥犱负闄愬埗浜嗕細鎶ラ敊锛屽鑷存暟鎹涪澶�
+     * @return 绾跨▼姹�
+     */
+    public ThreadPoolExecutor createThread(int initTread,int maxQueue){
+        this.threadPool = new ThreadPoolExecutor(initTread, initTread,
+                60L, TimeUnit.SECONDS,
+                new LinkedBlockingQueue<Runnable>());
+        this.maxQueue = maxQueue;
+        return this.threadPool;
+    }
+
+    /**鍒涘缓绾跨▼姹�
+     * @param initTread 鍒濆鍖栫嚎绋嬫睜
+     * @param initTread 鏈�澶х嚎绋嬫暟
+     * @param maxQueue 鏈�澶ч槦鍒楁柊澧為槦鍒楋紝娉ㄦ剰锛氳繖閲屽彧鍋氫紶鍊硷紝涓嶉檺鍒剁湡瀵�
+     *                  绾跨▼姹犻檺鍒堕槦鍒楋紝鍥犱负闄愬埗浜嗕細鎶ラ敊锛屽鑷存暟鎹涪澶�
+     * @return 绾跨▼姹�
+     */
+    public ThreadPoolExecutor createThread(int initTread,int maxTread,int maxQueue){
+        this.threadPool = new ThreadPoolExecutor(initTread,maxTread,60L,TimeUnit.SECONDS,queue);
+        this.maxQueue = maxQueue;
+        return this.threadPool;
+    }
+
+    /**閽堝鏈�澶х殑闃熷垪锛屽鏋滄病鏈夎秴杩囪繑鍥炴槸false鐨勶紝瓒呰繃灏辫繑鍥炴槸true鐨�
+     * 鍗曡繑鍥瀟rue鐨勬椂鍊欙紝灏变笉瑕佷紶鍏ラ槦鍒椾簡
+     * @param sleepMillisecond 鐫$湢锛屾绉掔,濡傛灉鏄┖鐨勶紝閭d箞鐩存帴杩斿洖
+     * @return 濡傛灉娌℃湁瓒呰繃杩斿洖鏄痜alse鐨勶紝瓒呰繃灏辫繑鍥炴槸true鐨�
+     */
+    public boolean noRund(Integer sleepMillisecond){
+        if(threadPool.getQueue().size() > maxQueue){
+            if(sleepMillisecond != null && sleepMillisecond > 0){
+                try{
+                    Thread.sleep(sleepMillisecond);
+                }catch (Exception e){
+                    logger.error("绾跨▼姹犲紑鍚潯鐪犲け璐ワ紒");
+                }
+            }
+            return true;
+        }else{
+            return false;
+        }
+    }
+
+    /**鍏抽棴*/
+    public void shutdown(){
+        this.threadPool.shutdown();
+    }
+
+    /**
+     * 鑷畾涔夌嚎绋嬩换鍔¢樆濉為槦鍒�. 鍦ㄦ椿璺冪嚎绋嬫暟灏忎簬鏈�澶ф敮鎸佺嚎绋嬫暟鐨勬儏鍐典笅锛屾柊浠诲姟涓嶆斁鍒伴槦鍒椾粠鑰屾縺鍙戠嚎绋嬫睜鍒涘缓鏂扮嚎绋嬪強鏃跺鐞�.
+     * 瑙e喅浣跨敤LinkedBlockingDeque鏃犻檺闃熷垪锛岀嚎绋嬫睜鍙湁鏍稿績绾跨▼鍦ㄥ鐞嗐�俶aximumPoolSize鏈惎浣滅敤鐨勯棶棰樸��
+     *
+     * @author chenjiahe
+     */
+    @Setter
+    private class ThreadTaskLinkedBlockingDeque<E> extends LinkedBlockingDeque<E> {
+        @Override
+        public boolean offer(E e) {
+            int activeThreadNum = threadPool.getActiveCount();
+            if (activeThreadNum < maxTread) {
+                return false;
+            }
+            return offerLast(e);
+        }
+    }
+
+    /****************************************************************************************/
+
+    public int getMaxQueue() {
+        return maxQueue;
+    }
+
+    public void setMaxQueue(int maxQueue) {
+        this.maxQueue = maxQueue;
+    }
+
+    public ThreadPoolExecutor getThreadPool() {
+        return threadPool;
+    }
+
+    public void setThreadPool(ThreadPoolExecutor threadPool) {
+        this.threadPool = threadPool;
+    }
+
+}

--
Gitblit v1.8.0