Reputation: 30578
Currently, I have a while loop for listening the port. When a socket accepta a connection, a Task, which implements a runnable
interface will be created, and is assigned to the LinkedList
named o_allTaskThreads
to store it. That is to let me have control, to stop them.
s = this.o_ServerSocket.accept();
System.out.println("connection established");
PortListenTask plt = new PortListenTask(s,this.o_config);
this.o_allTaskThreads.add(plt);
plt.run();
But I got some problems here, if the PortListenTask
is done/finished or have exception terminaled, it can't update the linkedLIst o_allTaskThreads
, how can I solve it? Thanks.
Upvotes: 1
Views: 839
Reputation: 3287
If you are using the newer version of Java (jdk 6), then look in to Future.
In case you are constrained and want an alternative approach, try using observer-observable pattern. You can use 'events' to achieve the same.
Upvotes: 0
Reputation: 800
You could have a boolean in every PortListenTask that indicates whether it's still running, then while looping through the threads check that boolean and if necessary remove it from the list using remove(int index). You can also make the list static, and remove the entry from the list from within the thread.
Upvotes: 0
Reputation: 2196
When the PortListenTask's run() method ends (normal execution completes), can it not update the linked list (I assume to remove itself) then? If you add a try / catch handler around the contents of your run() method then hopefully you can trap abnormal situations and help guide them through the same clean exit. You should always try and signal a thread to exit itself, rather than abort them externally, to allow such a clean exit.
Upvotes: 1
Reputation: 4693
The LinkedList
is not very good idea to store threads or jobs.
Try reading this Thread pools and this Thread pools and workers, that might help.
Upvotes: 2