+ " incrementing "
+ databaseBalanced + " by " + i);
}
synchronized(databaseBalancedLock) {
databaseBalanced += i;
}
// Cannot synchronize on cancel. Why?
}
public void run() throws InterruptedException {
Thread t;
nGuesses = 0;
found = false;
synchronized(this) {
for (int i = 0; i < nSearchers; i++) {
threads[i] = new TSDThread(new Searcher(this, i));
threads[i].start();
}
}
for (int i = 0; i < nSearchers; i++) {
threads[i].join();
System.out.println(threads[i].getName() + " joined.");
}
System.out.println("The answer is: " + answer +
", it took: " + nGuesses +
" guesses, and the database is...");
if (databaseBalanced == 0)
System.out.println("Consistant.");
else
System.out.println("Inconsistant!");
}
}
//
CancellationInterrupt/Searcher.java
import java.io.*;
import java.util.*;
import Extensions.*;
public class Searcher implements Runnable {
int
target;
Cancellation  cancel;
int
seed;
public void run() {
TSDThread self = (TSDThread) Thread.currentThread();
Random r = new Random(seed);
int guess;
System.out.println(self.getName() + " is searching...");
for (int i = 0; true; i++) {
try {
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home