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


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

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

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Posts
    24

    Question 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:

    Code:
    public static ArrayList chList;
    
    public void run() {
    chList = new ArrayList();
    chList.add(new serverListen());
    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.

  2. #2
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    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).
    Last edited by customwoodtek; 04-13-2005 at 04:41 PM.
    Java has 99 problems but a pointer ain't one

  3. #3
    Join Date
    Apr 2005
    Posts
    5
    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.

    --tony
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    If he is using 1.5 he can simply set the type of the ArrayList at initialization rather than casting, like so:

    ArrayList<Thread> chList = new ArrayList<Thread>();
    Java has 99 problems but a pointer ain't one

  5. #5
    Join Date
    Apr 2005
    Posts
    5

    Talking

    Yeah, but then you would have to adopt the other useless language "enhancements" like static imports and annotations. I guess if you stay away from those then it's ok.

    --tony

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center