|
|
От: | mik1 | |
| Дата: | 09.08.06 12:46 | ||
| Оценка: | |||
class CCC {
private final ConcurrentHashMap<String, Future<Void>> cache = new ConcurrentHashMap<String, Future<Void>>();
private static final ExecutorService exec = Executors.newFixedThreadPool(2);
public static void main2() throws Exception {
final CCC ccc = new CCC();
Thread impatientThread = new Thread() {
public void run() {
System.out.println("Impatient client requested for data");
try {
ccc.compute();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
System.out.println("Starting impatient client. ThreadId=" + System.identityHashCode(impatientThread));
impatientThread.start();
for (final int i : new int[] {1, 2}) {
new Thread() {
public void run() {
System.out.println("Starting client " + i + ". ThreadId=" + System.identityHashCode(this));
System.out.println("Client " + i + " requested for data");
try {
ccc.compute();
System.out.println("Client " + i + " received response");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
Thread.sleep(1000);
// System.out.println("Impatient client cancels request for data");
// impatientThread.interrupt();
exec.shutdown();
}
public Void compute() throws InterruptedException {
while (true) {
Future<Void> f = cache.get("1");
if (f == null) {
Callable<Void> eval = new Callable<Void>() {
public Void call() throws InterruptedException {
System.out.println("Computation started");
Thread.sleep(3000);
System.out.println("Computation ended");
return null;
}
};
System.out.println("1");
Future<Void> ft = exec.submit(eval);
System.out.println("2");
f = cache.putIfAbsent("1", ft);
if (f == null) f = ft;
}
try {
Thread.sleep(1000);
System.out.println("Print 1000, threadId=" + System.identityHashCode(Thread.currentThread()));
Thread.sleep(1000);
System.out.println("Print 2000, threadId=" + System.identityHashCode(Thread.currentThread()));
Thread.sleep(1000);
System.out.println("Print 3000, threadId=" + System.identityHashCode(Thread.currentThread()));
Thread.sleep(4000);
System.out.println("Print 4000, threadId=" + System.identityHashCode(Thread.currentThread()));
return f.get();
} catch (CancellationException e) {
System.out.println("CancellationException, threadId=" + System.identityHashCode(Thread.currentThread()));
cache.remove("1", f);
} catch (ExecutionException e) {
cache.remove("1", f);
System.out.println("ExecutionException, threadId=" + System.identityHashCode(Thread.currentThread()));
}
}
}
}Starting impatient client. ThreadId=5678233
Impatient client requested for data
1
2
Starting client 1. ThreadId=14746332
Client 1 requested for data
Computation started
Starting client 2. ThreadId=8568863
Client 2 requested for data
Print 1000, threadId=5678233
Print 1000, threadId=14746332
Print 1000, threadId=8568863
Print 2000, threadId=5678233
Print 2000, threadId=14746332
Print 2000, threadId=8568863
Print 3000, threadId=5678233
Computation ended
Print 3000, threadId=14746332
Print 3000, threadId=8568863
Print 4000, threadId=5678233
Print 4000, threadId=14746332
Client 1 received response
Print 4000, threadId=8568863
Client 2 received response