I have a multithreading application as follows:
class Distrobuter:
1- assign a task to a new Thread.
if(workers.size() >= MAXTHREADSALLOWED){
Worker worker = new Worker(task);
Thread t = new Thread(worker);
workers.add(t);
t.start();
}
2- remove Threads that have finished.
Iterator<Thread> itr = workers.iterator();
while (itr.hasNext()){
Thread t = itr.next();
if(t.getState() == Thread.State.TERMINATED){
itr.remove();
}
}
class Worker:
does a certain task
================================
However, there are some Threads that get removed even when their state is not terminated.
I know threads have been finished as they don't do any activities (no print out comming from them). But for some reason it does not satisfy this condition:
(t.getState() == Thread.State.TERMINATED)
So my question is:
- Is it possible that once a thread reaches end of its run method, it never change state to "TERMINATED"
- Is there any thing I'm missing?