JDK-6343393 : (thread) Thread.stop for main method thread ignored
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2005-10-28
  • Updated: 2010-04-02
  • Resolved: 2005-11-12
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 b61Fixed
Related Reports
Relates :  
Description
The VM-created and started main method thread (and only that thread) will not stop in response to Thread.stop being invoked for it.

The regression was caused by this test:

/net/sqe/global/nfs/sec/ws_6.0_int/security/src/Permissions/threadGroupStopTest

This regression was caused by a side effect of the fix for CR 4519200 ("(thread) Started
thread does not terminate immediately if it was stopped before").

The main method thread never gets it's Thread.start method invoked. Because of
that, Thread.state is never set to "true" for the main method thread,
sabotaging the logic in Thread.stop to correctly handle the "being stopped
before it was ever started" detail of the stop method's specification. This also means
that the check of the started field at the beginning of the Thread.start method is
redundant or we'd have been missing the part of the spec that says you can't start a
thread twice for the main method thread. The VM redundantly does this check and throws IllegalStateException for this case so this check in the library is only to prevent a
thread from being added to its ThreadGroup instance twice.

Comments
EVALUATION In my comments for reviewers for the 4519200 fix I wrote: > 1) The "started" field of Thread should eventually be eliminated > and replaced with use of the thread's Thread.State value, assuming > this is possible to do while maintaining spec compliance and > backwards compatibility. The VM does not use the started field and the implementation of the thread's state enum is the only right thing to use at this point.
28-10-2005

SUGGESTED FIX The fix is to replace the Thread.started field usage with threadStatus, the int that is the implementation of the Thread state enum. The threadStatus field is guaranteed to be zero by Thread construction and is set nonzero by the VM (either by Thread.start calling into the VM or, for the main method, when that method is started). *** j2se/src/share/classes/java/lang/Thread.java- Fri Oct 28 14:19:54 2005 --- Thread.java Fri Oct 28 14:16:32 2005 *************** *** 121,131 **** private char name[]; private int priority; private Thread threadQ; private long eetop; - private boolean started; // true iff this thread has been started /* Whether or not to single_step this thread. */ private boolean single_step; /* Whether or not the thread is a daemon thread. */ --- 121,130 ---- *************** *** 587,599 **** * started. * @see java.lang.Thread#run() * @see java.lang.Thread#stop() */ public synchronized void start() { ! if (started) throw new IllegalThreadStateException(); - started = true; group.add(this); start0(); if (stopBeforeStart) { stop0(throwableFromStop); } --- 586,597 ---- * started. * @see java.lang.Thread#run() * @see java.lang.Thread#stop() */ public synchronized void start() { ! if (threadStatus != 0) throw new IllegalThreadStateException(); group.add(this); start0(); if (stopBeforeStart) { stop0(throwableFromStop); } *************** *** 708,718 **** * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. */ @Deprecated public final void stop() { // If the thread is already dead, return ! if (started && !isAlive()) { return; } stop1(new ThreadDeath()); } --- 706,716 ---- * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. */ @Deprecated public final void stop() { // If the thread is already dead, return ! if ((threadStatus != 0) && !isAlive()) { return; } stop1(new ThreadDeath()); } *************** *** 780,790 **** if ((this != Thread.currentThread()) || (!(th instanceof ThreadDeath))) { security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION); } } ! if (started) { resume(); // Wake up thread if it was suspended; no-op otherwise stop0(th); } else { // Must do the null arg check that the VM would do with stop0 --- 778,788 ---- if ((this != Thread.currentThread()) || (!(th instanceof ThreadDeath))) { security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION); } } ! if (threadStatus != 0) { resume(); // Wake up thread if it was suspended; no-op otherwise stop0(th); } else { // Must do the null arg check that the VM would do with stop0 *** (#1 of 1): [ UNSAVED ]
28-10-2005