It's not a secret anymore!
The moose likes Threads and Synchronization and the fly likes help needed plz Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "help needed plz " Watch "help needed plz " New topic
Author

help needed plz

Johnson David
Greenhorn

Joined: May 22, 2005
Posts: 27
Hi friends ,

How many ways are there to stop a thread ?

what are they ?

thanx in advance

regards

johnson
Harald Kirsch
Ranch Hand

Joined: Oct 14, 2005
Posts: 37
My first click went here: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html

This immediately leads tohttp://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html


Harald.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by johnson david:
Hi friends ,

How many ways are there to stop a thread ?

what are they ?

thanx in advance

regards

johnson


That depends on the question. How many ways are there to stop any Thread? None that you should use. How many ways are there to code a Thread such that it can be killed? It's only limited by your imagination. The most common way is by having a loop in the run() method that checks a condition. Once the condition is no longer met the loop will break and the method will exit so that the Thread will die.

A Thread is an Object. It's a special kind of Object though as it starts a new thread of execution. This is the basis for multithreading and allows you to make use of multiple processors or to continue processing instructions even while waiting on a resource.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If the thread is looping while some condition is true you can make the condition false via a setter for example. (What Ken said)

If the thread is blocking on a wait(), you can interrupt() it.

If the thread is blocking on some other operation, like a db connect or a stream read, you may or may not be able to break that other operation. This is often a tough situation with no easy solution.

More?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
But interrupt doesn't stop a thread does it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

Originally posted by Ken Blair:
But interrupt doesn't stop a thread does it?


No it doesn't -- but I think Stan was just elaborating the "setting the exit condition" example. Interrupting it will causes it to break out of wait() and hence, can check whether it should exit.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help needed plz