消费者消费消息它有一个工作池的概念,所以现在研究一下WorkPool的源码,看一看可以学到什么技巧。
-
这个实现是一个线程安全的类
-
WorkPool<K,W> K : 客户端类型, W : 工作项目类型
1、成员变量
- MAX_QUEUE_LENGTH = 1000 队列最大为1000
- ready = new SetQueue() 准备好客户端
- inProgress = new HashSet() 正在处理客户端
- Map<K, VariableLinkedBlockingQueue> = new HashMap 真正的池子,包括注册客户端和它们工作队列
- unlimited = new HashSet() 不限制队列大小
- enqueueingCallback 入队回调函数 (使用BiConsumer)
2、构造函数
-
定义enqueueingCallback 的函数,入队工作项
-
public WorkPool(final int queueingTimeout) { if (queueingTimeout > 0) { this.enqueueingCallback = (queue, item) -> { try { boolean offered = queue.offer(item, queueingTimeout, TimeUnit.MILLISECONDS); if (!offered) { throw new WorkPoolFullException("Could not enqueue in work pool after " + queueingTimeout + " ms."); } } catch (InterruptedException e) { Thread.
-

最低0.47元/天 解锁文章
1503

被折叠的 条评论
为什么被折叠?



