美文网首页
第 84 条:不要依赖于线程调度器

第 84 条:不要依赖于线程调度器

作者: 综合楼 | 来源:发表于2021-08-21 14:47 被阅读0次
不要依赖于线程调度器.jpeg
// Awful CountDownLatch implementation - busy-waits incessantly!
public class SlowCountDownLatch {
    private int count;
    public SlowCountDownLatch(int count) {
        if (count < 0)
            throw new IllegalArgumentException(count + " < 0");
        this.count = count;
    }
    public void await() {
        while (true) {
            synchronized(this) {
                if (count == 0)
                    return;
            }
        }
    }
    public synchronized void countDown() {
        if (count != 0)
            count--;
    }
}

相关文章

网友评论

      本文标题:第 84 条:不要依赖于线程调度器

      本文链接:https://www.haomeiwen.com/subject/lfmlsltx.html