JDK-4519200 : (thread) Started thread does not terminate immediately if it was stopped before
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.1.8,1.4.0,5.0,6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2001-10-25
  • Updated: 2005-10-14
  • Resolved: 2005-10-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b57Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
Name: skR10005			Date: 10/25/2001


The thread does not terminate immediately if it was stopped before it was started.
The API spec for method Thread.stop reads:
  "It is permitted to stop a thread that has not yet been started. If the thread 
   is eventually started, it immediately terminates."

To reproduce the failure the following simple example can be used:
=================================================================
public class test {
    public static void main(String[] argv) {
        A a = new A();
        a.stop();
        a.start();
        try {
            a.join();
        } catch (Throwable e) {
            System.out.println("Unexpected exception" + e);
        }
        if(a.state) {
            System.out.println("Passed");
        } else {
            System.out.println("Failed");
        }
    }
}

class A extends Thread {
  public volatile boolean state = true;
  public void run() {
      state = false;
  }
}
=================================================================
$java -version
java version "1.4.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)
$java -cp . test
Failed


======================================================================
From 6195215 (closed as a dupe of this CR):

A licensee reports the thread.stop() behavior does not follow the specifications
in thread.stop() API Document(although stop() is deprecated....)

According to the API document of thread.stop(),

.....
It is permitted to stop a thread that has not yet been started. If the thread is eventually started, it immediately terminates.
....

They hoped  "when  stopped thread starts , the thread terminates without doing anything."

Comments
EVALUATION This defect correction brings implementation in line with the specification but does nothing to make the stop method safer to use: it is just as unsafe as it ever was! See 'Why Are Thread.stop (et all) Deprecated?' linked to from the Thread.stop javadoc for more details. The implementation converts a stop before start into a start back to back with a stop. The VM's termination of the thread races it's startup so there is no guarantee the thread will catch an exception before termination. That is, an uncaught exception handler callback will not take place if the thread terminates on its own before the VM can deliver ThreadDeath or the user specified exception to it. This would usually only be true for an empty run method. The latency of thread stoppage will typically be 1-3 bytecodes if the new thread is interpreted or the next point at which it reaches a loop back edge or method return if compiled. Resource contention between the new thread and the stopping thread can add additional delay.
10-10-2005

EVALUATION While this is clearly a bug, it concerns a deprecated API. ###@###.### 2001-11-06
06-11-2001