Results 1 to 7 of 7
  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default How an array of Threads works...

    Greetings. I'm looking for some information regarding how Threads and arrays can work together. I've created a Thread class that extends Threads. I have an array of this class and as an event happens I trigger this thread to start. Once the Thread is finished, I attempt to remove its position in the array such:
    Java Code:
    classThreadExample example = new classThreadExample[5]
    
    example[0].start(); //a
    example[1].start(); //b
    example[2].start(); //c
    example[3].start(); //d
    
    // Now say I want to remove Thread b
    example[1]=example[2];
    example[2]=example[3];
    
    // Now I want to use example[3] space as a new event happens...
    
    example[3] = new classThreadExample(passInSomeStuff);
    example[3].start();  //Produces illegal Thread State
    Please note that the code above is just a short remaking of what i'm trying to do and is not the exact code I have setup, but that is the general idea.

    Now I know that once a Thread has been started, it cannot be started again, even when it has finished. What I am not sure of is why this thread finishes but does not disappear from the array. I've even tried setting the array spot to null and it will not produce any different results.

    If you know of a proper way to deal with this kind of situation, any insight would be useful. Thanks.
    Last edited by Moncleared; 09-08-2009 at 11:55 PM. Reason: More information might have been needed, added...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Clients are connecting to a server. As they are connecting a thread is created for the server to interact with them, also a counter is increased so the next client to connect is put in the next array slot.

    All of this works fine.

    Now that I can have clients connect and interact with the server, I want to anticipate them leaving the server, and I don't want to continue my route of using the next array spot. So if 5 clients connect, 0 1 2 3 4, client 2 disconnects, i do not want let the next client connecting us 5, i want to take client 2 out, producing, 0 1 2 3, so the next client connecting uses 4 instead of 5.

    I hope that makes sense. I tried drawing it out in my pseudo best i could.

    The way I have it working now is:
    a client connects (counter++)
    a client connects (counter++)
    a client disconnects (no change)
    a client connects (counter++)

    So my array is continuously increasing, instead of being dynamic based on how many clients are connecting/disconnecting.

  4. #4
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    So I think I can simplify my question a little bit I guess.

    If I have an array location that holds a Thread:

    ex[0] = new exampleThread();
    ex[0].start();

    Now this thread does some work and it FINISHES...

    How do I re-use ex[0] for a new Thread?

    Java Code:
    exampleThread ex = new exampleThread[5];
    ex[0].start();
    
    //ex[0] has finished processing
    
    ex[0] = new exampleThread();
    ex[0].start() // produces error

  5. #5
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Use a List and not an array.

    One way: Have your thread class hold a static concurrent List (like ConcurrentLinkedQueue) of your threads. When you start the thread it adds itself to the List and when it is finished it removes itself from the list.

    Another way: Have a concurrent list and pass it to the threads constructor to add and remove itself from that list.
    Last edited by mrmatt1111; 09-09-2009 at 12:52 AM.
    My Hobby Project: LegacyClone

  6. #6
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Thanks for the reply.

    I'm not very familiar with Concurrency and Threads, maybe a small example would help? Also reading the java.util.concurrent in the mean time.

    Thanks

  7. #7
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    A very rough basic example:

    Java Code:
    import java.util.concurrent.ConcurrentLinkedQueue;
    
    class MyThread extends Thread {
       private ConcurrentLinkedQueue<MyThread> activeThreads;
       
       private final Object lock = new Object();
       
       private static int threadCount = 0;
       private final int threadId;
    
       public MyThread(ConcurrentLinkedQueue<MyThread> activeThreads) {
          this.activeThreads = activeThreads;
          
          synchronized(lock) { //make sure to protect shared data
             threadId = threadCount++;
          }
       }
    
       public void start() {
          System.out.println("["+threadId + "] Adding Thread to Active List");
          activeThreads.add(this);
          super.start();
       }
    
       public void run() {
          try {
             System.out.println("["+threadId + "] Thread sleeping for awhile.");
             sleep((int)Math.round(Math.random() * 5000) + 1000);  //sleep for a random amount
             System.out.println("["+threadId + "] Thread woke up.");
          }
          catch(Exception e) { e.printStackTrace(); }
    
          System.out.println("["+threadId + "] Removing Thread from Active List");
          activeThreads.remove(this);
       }
    }
    
    public class ThreadsAreFun{
       public static void main(String args[]) {
          ConcurrentLinkedQueue<MyThread> activeThreads = new ConcurrentLinkedQueue<MyThread>();
    
          while(true) {
             if(activeThreads.size() < 5) {
                new MyThread(activeThreads).start();
             }
             try { Thread.sleep(25); } catch(Exception e) {} //briefly pause as to not use all the CPU
          }
       }
    }
    My Hobby Project: LegacyClone

Similar Threads

  1. Replies: 2
    Last Post: 11-18-2008, 10:44 PM
  2. What are the hot java frames works on demand
    By mallaravi in forum Web Frameworks
    Replies: 1
    Last Post: 10-28-2008, 01:34 PM
  3. how compareTo Method works
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2008, 07:40 PM
  4. Replies: 0
    Last Post: 06-04-2008, 01:24 PM
  5. Project works in debugger, not after compiling
    By Fleur in forum New To Java
    Replies: 11
    Last Post: 05-29-2008, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •