62,635
社区成员




public class Ziyuan<R> {
private Hashtable<R, Boolean> tab = new Hashtable<R, Boolean>();
public void add(R r) {
synchronized (tab) {
tab.put(r, false);
}
}
//使用这个资源
public void use(R r) {
synchronized (tab) {
if (tab.containsKey(r)) {
tab.put(r, true);
tab.notifyAll();
}
}
}
//停止使用资源
public void release(R r) {
synchronized (tab) {
if (tab.containsKey(r)) {
tab.put(r, false);
tab.notifyAll();
}
}
}
//关闭系统 需要等待所有资源停止使用
public void close() throws InterruptedException {
synchronized (tab) {
while (true) {
boolean able = true;
for (R r : tab.keySet()) {
if (tab.get(r)) {
able = false;
break;
}
}
if (!able) {
tab.wait(10000);
} else {
System.out.println("可以关闭了!");
break;
}
}
}
}
}