This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes how to kill or stop a thread in java2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "how to kill or stop a thread in java2" Watch "how to kill or stop a thread in java2" New topic
Author

how to kill or stop a thread in java2

allyou wantis
Greenhorn

Joined: May 01, 2004
Posts: 2
i have certain number thread running and i want to stop some of them.Is there any way to do it in java2.
Stefan Krompass
Ranch Hand

Joined: Apr 29, 2004
Posts: 75
Hi,
my solution looks something like this:

cu
Stefan
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome both of you to the Ranch!
allyou wantis please read our naming policy and adjust your displayed name accordingly, thank you.
The running variable should have been declared volatile.


SCJP2. Please Indent your code using UBB Code
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
Can't you use interrupt() method?
Having stopWork() is great but what if this is not ur thread(does not have the method) or the thread is in sleep/wait mode, or waiting for IO (for example Socket s =server.accept())
clio katz
Ranch Hand

Joined: Apr 30, 2004
Posts: 101
right.

both good suggestions, it seems to me!

stefan offers a nice, healthy-looking thread template, and yaroslav offers good advice about making provisions for (un)intended exit/shutdown

(i.e. "try" the 'do something' loop, "catch" InterruptedException, and possibly execute a "finally" block for clean shut-down)

hth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to kill or stop a thread in java2
 
Similar Threads
Oh boy !!!! Threads again.
When should I appear for SCJP?
Does stop() method still can be used in Java2?
Just confirm it...
threads