8. 例 要素が 1 つのコンテナ
public class MonoContainer<T> {
private T value;
public synchronized T take();
public synchronized void put(T v);
private synchronized boolean isEmpty();
}
9. これは動かない ...
public synchronized T take() {
while (isEmpty()) {}
T v = value; value = null;
return v;
}
public synchronized void put(T v) {
while (!isEmpty()) {}
value = v;
}
10. 動くけど ......
public T take() {
while (true) {
try { Thread.sleep(DURATION);
} catch (InterruptedException ex) {}
synchronized (this) {
if (!isEmpty()) {
T v = value; value = null;
return v;
}
}
}
}
41. public class Main {
public static void main(String[] args) {
switch (args[0].toLowerCase()) {
case "fiber":
new AppServer().start(20080,
cmd -> Fiber.schedule(cmd));
break;
case "thread":
new AppServer().start(20081,
Executors.newFixedThreadPool(
Integer.parseInt(args[1])));
}
}
}