java手写简易版自定义线程池 V1
创始人
2024-11-14 22:33:20
import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock;  public class DdqThreadPool {     private static final class DdqThread extends Thread {         private Runnable runnable;         private final ReentrantLock reentrantLock = new ReentrantLock();         private final Condition condition = reentrantLock.newCondition();          public void setRunnable(Runnable runnable) {             try {                 reentrantLock.lock();                 this.runnable = runnable;                 condition.signalAll();             } catch (Exception exception) {                 throw new RuntimeException(exception);             } finally {                 reentrantLock.unlock();             }         }          @Override         public void run() {             while (true) {                 try {                     reentrantLock.lock();                     while (runnable == null) condition.await();                     runnable.run();                     runnable = null;                 } catch (Exception exception) {                     throw new RuntimeException(exception);                 } finally {                     reentrantLock.unlock();                 }             }          }     }      private List threads;      private AtomicInteger threadSize;      private DdqThreadPool(List ddqThreads) {         this.threads = ddqThreads;         this.threadSize = new AtomicInteger(ddqThreads.size());     }      public static DdqThreadPool newDdqThreadPool(int maxThreadSize) {         if (maxThreadSize < 1) throw new IllegalArgumentException("线程数量必须大于0");         List ddqThreads = new LinkedList<>();         for (int idx = 0; idx < maxThreadSize; idx++) {             DdqThread ddqThread = new DdqThread();             ddqThread.start();             ddqThreads.add(ddqThread);         }         return new DdqThreadPool(ddqThreads);     }      public void execute(Runnable runnable) {         if (runnable == null) throw new IllegalArgumentException("runnable不能为null");         int oldThreadSize1 = threadSize.get();         if (oldThreadSize1 < 1) return;         if (!threadSize.compareAndSet(oldThreadSize1, oldThreadSize1 - 1)) return;         DdqThread idleThread = threads.remove(0);         idleThread.setRunnable(runnable);         //以下三段代码可能还存在bug,因为idleThread.setRunnable(runnable)后,由于是异步执行,可能会存在着线程没执行完,但是忙碌线程又被添加回了可用线程集合         int oldThreadSize2;         while (!threadSize.compareAndSet((oldThreadSize2 = threadSize.get()), oldThreadSize2 + 1)) ;         threads.add(idleThread);     } }  

V2版本请点击此处

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...