多线程任务调度与同步机制解析
1. 工作窃取双端队列(Work - Stealing Dequeues)
工作窃取算法是多线程任务调度中的一种重要策略,其中涉及到双端队列(Dequeues)的操作。下面是 UnboundedDEQueue 类中 popTop() 和 popBottom() 方法的代码:
public Runnable popTop() {
int oldTop = top.get();
int newTop = oldTop + 1;
int oldBottom = bottom;
int size = oldBottom - oldTop;
if (size <= 0) return null;
Runnable r = tasks.get(oldTop);
if (top.compareAndSet(oldTop, newTop))
return r;
return null;
}
public Runnable popBottom() {
bottom--;
int oldTop = top.get();
int newTop = oldTop + 1;
int size = bottom - oldTop;
if (size < 0) {
bottom = oldTop;
return null;
}
Runnable r = tasks.get(botto
超级会员免费看
订阅专栏 解锁全文

10万+

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



