• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to kill or stop a thread in java2

 
allyou wantis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
my solution looks something like this:

cu
Stefan
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic