The following extreme test case shows that stop() is ignored if called too
quickly after start. The program creates a bundle of threads and stops them
all. After stopping each one it prints the value of isAlive(). For me, I get all
the threads being reported alive even though all should be reported dead.
I can imagine there being a concurrency/scheduling problem in stopping
a thread before it has really been started. However, since the thread has
already been created, in the worst case, it should be possible to store a boolean
in the thread state that is checked when appropriate.
Whatever the problem or solution, this is a serious problem if stop() is really
to be supported, since in a multi-threaded world, it is difficult to know if the
call of stop has succeeded or not?
The test case is extreme in calling stop quite so soon after start, but the test case
is refined from a real case where a real program was refusing to exit because some
threads had not died even though they had been stopped.
-- Jon
----------------------------
public class Bug extends Thread
{
public static void main(String[] args) {
Bug[] bugs = new Bug[10];
for (int i = 0; i < bugs.length; i++) {
bugs[i] = new Bug();
bugs[i].start();
System.out.println("bugs[" + i + "] = " + bugs[i]);
bugs[i].stop();
System.out.println("bugs[" + i + "] " +
(bugs[i].isAlive() ? "alive" : "dead"));
}
System.out.println("main() preparing to exit");
int n = Thread.activeCount();
if (n > 0) {
System.out.println(n + " threads outstanding");
Thread[] tarray = new Thread[n];
n = Thread.enumerate(tarray);
for (int i = 0; i < n; i++) {
System.out.println(tarray[i] + " " +
(tarray[i].isAlive() ? "alive" : "dead"));
}
}
System.out.println("main() really exiting");
}
public void run() {
try {
wait();
}
catch (InterruptedException e) {
}
}
}
quickly after start. The program creates a bundle of threads and stops them
all. After stopping each one it prints the value of isAlive(). For me, I get all
the threads being reported alive even though all should be reported dead.
I can imagine there being a concurrency/scheduling problem in stopping
a thread before it has really been started. However, since the thread has
already been created, in the worst case, it should be possible to store a boolean
in the thread state that is checked when appropriate.
Whatever the problem or solution, this is a serious problem if stop() is really
to be supported, since in a multi-threaded world, it is difficult to know if the
call of stop has succeeded or not?
The test case is extreme in calling stop quite so soon after start, but the test case
is refined from a real case where a real program was refusing to exit because some
threads had not died even though they had been stopped.
-- Jon
----------------------------
public class Bug extends Thread
{
public static void main(String[] args) {
Bug[] bugs = new Bug[10];
for (int i = 0; i < bugs.length; i++) {
bugs[i] = new Bug();
bugs[i].start();
System.out.println("bugs[" + i + "] = " + bugs[i]);
bugs[i].stop();
System.out.println("bugs[" + i + "] " +
(bugs[i].isAlive() ? "alive" : "dead"));
}
System.out.println("main() preparing to exit");
int n = Thread.activeCount();
if (n > 0) {
System.out.println(n + " threads outstanding");
Thread[] tarray = new Thread[n];
n = Thread.enumerate(tarray);
for (int i = 0; i < n; i++) {
System.out.println(tarray[i] + " " +
(tarray[i].isAlive() ? "alive" : "dead"));
}
}
System.out.println("main() really exiting");
}
public void run() {
try {
wait();
}
catch (InterruptedException e) {
}
}
}
- relates to
-
JDK-4519200 (thread) Started thread does not terminate immediately if it was stopped before
- Resolved
-
JDK-4168013 JVM_StopThread should not set the stillborn bit for running threads
- Closed