Results 1 to 7 of 7
Thread: How an array of Threads works...
- 09-08-2009, 11:52 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
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:
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.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
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...
-
What is the overall purpose of this framework?
- 09-09-2009, 12:07 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
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.
- 09-09-2009, 12:25 AM #4
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
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
- 09-09-2009, 12:45 AM #5
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
- 09-09-2009, 12:54 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
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
- 09-09-2009, 01:40 AM #7
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
-
Can someone please help fix this code so the program works? (2D arrays)
By busdude in forum New To JavaReplies: 2Last Post: 11-18-2008, 10:44 PM -
What are the hot java frames works on demand
By mallaravi in forum Web FrameworksReplies: 1Last Post: 10-28-2008, 01:34 PM -
how compareTo Method works
By nanaji in forum Advanced JavaReplies: 1Last Post: 06-22-2008, 07:40 PM -
Second level caching in Hibernate: when it works?
By leonus in forum JDBCReplies: 0Last Post: 06-04-2008, 01:24 PM -
Project works in debugger, not after compiling
By Fleur in forum New To JavaReplies: 11Last Post: 05-29-2008, 09:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks