...
- If
ThreadDeath
is left uncaught, it allows the execution of afinally
block which performs the usual cleanup operations. Use of theThread.stop()
method is highly inadvisable because of two reasons. First, no particular thread can be forcefully stopped because an arbitrary thread can catch the thrownThreadDeath
exception and simply choose to ignore it. Second, abruptly stopping threads abruptly a thread results in the release of all the associated monitorslocks that it has acquired, violating the guarantees provided by the critical sections. Moreover, the objects end up in an inconsistent state, nondeterministic non-deterministic behavior being a typical outcome.
...
Code Block | ||
---|---|---|
| ||
public class Container implements Runnable {
private final Vector<String> vector = new Vector<String>();
public Vector<String> getVector() {
return vector;
}
public void run() {
String string = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
do {
System.out.println("Enter another string");
try {
string = in.readLine();
} catch (IOException e) {
// Forward to handler
}
vector.add(string);
} while (!"END".equals(string));
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Container());
thread.start();
Thread.sleep(5000);
thread.stop();
}
}
|
...