If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Holding on to Threads/Runnables (ArrayList) to be able to .STOP them

Coding Style: NetBeans IDE 4.0 Beta2 (Java)

My code (a thread, actually "implements runnable") spawns threads (runnables) to handle listening for incomming messages and stores them in an ArrayList (I could not think of a better way, please propose a better method if possible).
This is done as such:

This should allow me, when I want to close my main thread (runnable), to close all my listener threads (serverListen runnable) using the ArrayList (chList).
-- Quick question, I can do a .STOP on my listener threads (serverListen runnable) from this main thread right? (I create a public void stop(){ thread.stop;} function in my serverListen threads, I can do this right? if not how am I supposed to kill a thread from this scope? ---

So my problem is unlike a normal Array the ArrayList doesn't seem to allow me to go through each index and do a serverListen.stop()

So my goal is to do something like
for (int i = 0; i < chList.size; i++)
{
chList[i].stop()
}
Of course this is not working for many reasons (doesn't like "chList[i]", and "chList[i].stop" doesn't seem to be recognized either).
Is there anyway to do this? A better way?
Any help would be GREATLY appreciated, thanks.

it would help to see the code for serverListen, or is this the code for serverListen? Also class names should start with capital letters, not extremly important but a good programming practice to help with readability. Finally, as i said in one of your other posts, you should not be using the depreceated stop method.

EDIT: Another thing i noticed, you are using ArrayList incorrectly. You cannot access an ArrayList the same as an array. That is why it has a problem with chList[i]. It should be chList.get(i).

Well first off, your ArrayList is a list of Thread objects. So performing

for (int i = 0; i < chList.size; i++)
{
chList[i].stop()
}

won't work. You would need to say ((Thread)chList.get(i)).stop();.
There is also a problem with this, stop() is deprecated, meaning its not a safe solution to stop the thread. For a better solution read safer thread control.

I think you may want to look into the ThreadGroup class as this can provide you with methods to enumerate through the group's members and determine the state of them, as well as interrupt them.

I attached a two simple java files that show a Main thread and 5 worker threads.