On Mon, 22 May 2000, Jason Baker wrote:
> Mo DeJong <[EMAIL PROTECTED]> writes:
>
> > I was under the impression that methods like Thread.stop() were
> > removed from the JDK or replaced with no-ops. I seem to remember
> > that they were never implemented in Netscape's JVM. The whole
> > concept of stoping a thread seems like a bad idea, a thread
> > should expire due to natural causes.
There is (or was, I have the docs for a JDK 1.3 beta) a nice document
explaining the various issues on thread stopping linked from the
method summary of java.lang.Thread. It is called "Why Are Thread.stop,
Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?", and
should be the file jdk1.3/docs/guide/misc/threadPrimitiveDeprecation.html in
the JDK 1.3 documentation. Essentially, it makes the point, that unless you
really, really know what you're doing, Thread.stop() leaves your objects in a
damaged state, and leads to unleasant surprises.
Thread.stop(Throwable) is even worse: in addition anyone can pass any exception
to your thread, even exceptions it's not prepared to catch ...
There is an implementation of Thread.stop and other deprecated Thread methods
in the JDKs, it's just marked deprecated since JDK 1.2.
In Peter Haggar's "Practical Java" he talks about thread stopping in praxes 57
& 58, and essentially recommends the same things Sun does: don't use
Thread.stop(). If you have to do stop threads, use polling on a volatile
variable (since threads can copy local variables), or use synchronized methods
to access it. If the stop variable is true, then clean up your resources, put
the object in a safe state, and exit via the Thread.run method(). The
disadvantage is: the thread does not stop immediately, it has to read the stop
variable first.
If that's an issue, like for threads that wait for a long period, than you
should additionally use interrupts. There are examples of all that in Sun's
document described above.
> Thread.stop is dangerous, but without a process model there is really
> no alternative. I'm using it to halt infinite loops, which is why I
> need the stack trace.
I'd say, try to use polling. Something like this should do it:
class InfiniteLoopThread extends Thread {
private volatile boolean quit;
public void stop() {
quit = true;
}
public void safeStop(Throwable t) {
quit = true;
// if you need the stack trace
// to see who stopped the thread
t.printStackTrace();
}
public void run() {
while (!quit) {
// your infinitely looping code
}
// oops! turned out to be finitely looping!
// do the clean up.
// dump the stack trace
// if you still need it
this.dumpStack();
}
}
>
> Both Godmar and Pat use Thread.stop to implement safe termination, so
> given that it is part of Kaffe, what should the call do?
According to the JDK 1.3 API documentation the Thread.stop() method should look
something like this:
public void stop() {
SecurityManager sm;
sm = System.getSecurityManager())
if(sm) {
sm.checkAccess(this);
if(this != currentThread()) {
sm.checkPermission(new RuntimePermission("stopThread"));
}
}
throw (new ThreadDeath());
}
Thread.stop(Throwable) should be very similar.
There is one thing that the JDK 1.3 documentation says about printing stack
traces:
The top-level error handler that reacts to otherwise uncaught exceptions does
not print out a message or otherwise notify the application if the uncaught
exception is an instance of ThreadDeath.
I think your 'Die' code shows a bug or two in the JDK 1.2.2 and JDK 1.1.7. It
should generate stack traces since you're throwing Errors around, not
ThreadDeaths. Unless of course, Sun's JVM generally doesn't print stack traces
on Errors or Throwables, but I haven't found anything supporting that in the
documentation for java.lang.Error or java.lang.Throwable.
Last time I looked at Kaffe's threads, it didn't do
the SecurityManager checks, so Kaffe is not 'fully compliant' with JDK 1.3
specification (or 1.2, for that matter) either.
hope this helps,
Dali
__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com