How to interrupt a specific Thread.

Imagine that I have about 5 threads; an each thread has an unique Name. I would like to know how can I select a specific Thread in order to interrupt it.
[160 byte] By [lucaspintoa] at [2007-11-27 4:01:24]
# 1
t.interrupt();Where t refers to the thread.
DrLaszloJamfa at 2007-7-12 9:06:06 > top of Java-index,Java Essentials,Java Programming...
# 2

If you aren't storing your threads when you create them - you can ask the ThreadGroup they're in, for all of its Threads, and then look for the specific thread by iterating and asking thread.getName() for each.

Here's a description of how to find all threads in your app, if you don't even have the ThreadGroup handy:

http://jug.org.ua/wiki/display/JavaAlmanac/Listing+All+Running+Threads

Grant

ggaineya at 2007-7-12 9:06:06 > top of Java-index,Java Essentials,Java Programming...
# 3
I should have added that Thread & ThreadGroup have enumerate methods,although I would directly hand onto a reference when creating the thread, if possible.
DrLaszloJamfa at 2007-7-12 9:06:06 > top of Java-index,Java Essentials,Java Programming...
# 4
A thread name doesn't have to be unique?Kaj
kajbja at 2007-7-12 9:06:06 > top of Java-index,Java Essentials,Java Programming...
# 5

> A thread name doesn't have to be unique?

True - but if I'm writing code that depends on a Thread's name, then I can arrange for them to be so. In fact, I may even rely on the non-uniqueness, so I can use the examine-all-threads process to interrupt an entire class of threads.

Although I think that holding onto Thread references and just whacking the one you want is probably the right answer for the OP...

G

ggaineya at 2007-7-12 9:06:06 > top of Java-index,Java Essentials,Java Programming...
# 6
All suggestions were very helpful.Thanks a lot!Lucas
lucaspintoa at 2007-7-12 9:06:06 > top of Java-index,Java Essentials,Java Programming...