栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

具有有限队列的线程池

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具有有限队列的线程池

我希望有一个回调机制,当达到队列大小限制时,我会在其中收到通知…

我不会将执行器子类化,但会将执行

BlockingQueue
器使用的子类化。像下面这样的东西应该起作用。
checkUnsaturated()
如果删除条目并将某人放回原处,则代码中存在竞争条件。如果这些条件需要完善,则可能必须在队列上进行同步。另外,我也不知道执行器实现使用什么方法,因此您可能不需要覆盖其中的一些方法。

public class ObservableBlockingQueue<E> extends linkedBlockingQueue<E> {     private ISaturatedPoolObserver observer;     private int capacity;     public ObservableBlockingQueue(ISaturatedPoolObserver observer,         int capacity) {         super(capacity);         this.observer = observer;         this.capacity = capacity;    }    @Override    public boolean offer(E o) {        boolean offered = super.offer(o);        if (!offered) { observer.onSaturated();        }        return offered;    }    @Override    public boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException {        boolean offered = super.offer(o, timeout, unit);        if (!offered) { observer.onSaturated();        }        return offered;    }    @Override    public E poll() {        E e = super.poll();        if (e != null) {  checkUnsaturated();        }        return e;    }    @Override    public E poll(long timeout, TimeUnit unit) throws InterruptedException {        E e = super.poll(timeout, unit);        if (e != null) {  checkUnsaturated();        }        return e;    }    @Override    public E take() throws InterruptedException {        E e = super.take();        checkUnsaturated();        return e;    }    @Override    public boolean remove(E e) throws InterruptedException {        boolean removed = super.remove(e);        if (removed) { checkUnsaturated();        }        return removed;    }    private void checkUnsaturated() {        if (super.size() * 100 / capacity < UNSATURATED_PERCENTAGE) { observer.onUnsaturated();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392482.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号