boundedovertakingallocator.java
来自「演示win32的socket 通讯 八皇后的改进算法 并发Concurren」· Java 代码 · 共 40 行
JAVA
40 行
//********************************************************
//
// BoundedOvertakingAllocator Class
//
package concurrency.golf;
public class BoundedOvertakingAllocator implements Allocator{
private int available;
private int turn = 0;
private int next = 0;
private int bound;
private int overtaken =0;
public BoundedOvertakingAllocator(int n, int b)
{ available = n; bound = b;}
synchronized public void get(int n)
throws InterruptedException{
int myturn = turn; ++turn;
boolean overtakenMe = false;
while (n>available || (overtaken>0 && !overtakenMe)) {
wait();
if ((next>= (myturn + bound)) && !overtakenMe) {
overtakenMe = true; ++overtaken;
}
}
if (overtakenMe) --overtaken;
++next; available -= n;
notifyAll();
}
synchronized public void put(int n) {
available += n;
notifyAll();
}
}
//********************************************************
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?