File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes how can I kill a thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "how can I kill a thread?" Watch "how can I kill a thread?" New topic

how can I kill a thread?

tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
I have a thread executing inside of my applet and I've read that I shouldn't use the stop method of a thread to end it. Here's the scenario.
I have one main .asp page that is loaded with every other page. Inside of this .asp page is my applet. So, the applet gets loaded every time someone changes pages. I need to kill the thread and restart it every time the person changes pages. When a certain time has arrived, I show a message box alerting the person that the time has arrived. The problem is that right now, I'm using the applets stop method...

... to try and end the thread the way I read about. But it's not working. Is there another way to do it without resorting to the deprecated stop method of the thread? Thanks.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Your stop method simple drops the reference to the already running thread (at least that's what I gather from your post). That doesn't actually stop anything, it just relies on the garbage collector to one day come around and clean things up; but who knows when the garbage collector will come?
Perhaps you want to redesign your thread a bit, so that it is performing inside of a while loop that constantly checks if it should keep running, and stops running when the check tests false.
Here's a potential outline for what I'm talking about:

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: how can I kill a thread?
It's not a secret anymore!