📄 serverlist.java
字号:
import java.lang.*;
import java.util.*;
class ServerList {
private Collection<Notify> threadList =
new ArrayList<Notify>();
private int counter = 0;
// Get the lock on theadList, and wait until the counter is zero - that
//is, no reads are taking place. Then it's safe to add the thread.
public synchronized void add(Notify item) {
try {
while (counter > 0) {
wait();
}
threadList.add(item);
}
catch (InterruptedException e) {
System.out.println("Addition interrupted.");
}
finally{
notifyAll();
}
}
// Similarly for removal.
public synchronized void remove(Notify item) {
try {
while (counter > 0) {
wait();
}
threadList.remove(item);
}
catch (InterruptedException e) {
System.out.println("Removal interrupted.");
}
finally {
notifyAll();
}
}
// Similarly for changing counter
public synchronized void incCounter() {
counter++;
notifyAll();
}
public synchronized void decCounter() {
counter--;
notifyAll();
}
//This is because it would be too much effort to make this class implement
//Collection, return it's own Iterator etc. etc...\
//Note it is *not* a bug that it isn't synchronized
public Collection getCollection() {
return threadList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -