I have a class extending Thread class as follows.
public class RemoteDataSynchTaskProcessor extends Thread {
private GDRType taskGDR;
public RemoteDataSynchTaskProcessor(GDRType taskGDR) {
this.taskGDR = taskGDR;
}
private void execute() {
try {
try {
RemoteClient.runDataSynchTask(taskGDR);
} catch (RemoteException re) {
//.....
} catch (NotBoundException e) {
//......
}
processResult(false, "Success");
} catch (DTSException de) {
//....
}
}
public void run() {
Observer.incrementNumOfDataSyncyProcessor();
try {
execute();
} finally {
Observer.decrementNumOfDataSynchProcessor();
}
}
After this thread’s execution is over there appears another thread named: Thread [pool-34-thread-1] (Running)
I have no idea why this thread appears and never terminates, and i end up with thousands of threads after a couple of day with pool number increasing every time ( e.g. Thread [pool-2500-thread-1])
If if debug and suspend execution of these generated threads, the stack is as follows:
Thread [pool-1-thread-1] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: 156
AbstractQueuedSynchronizer$ConditionObject.await() line: 1987
LinkedBlockingQueue<E>.take() line: 399
ThreadPoolExecutor.getTask() line: 947
ThreadPoolExecutor$Worker.run() line: 907
Thread.run() line: 662
What are these threads, how can i get rid of them ?
EDIT: Seems like it has something to do with closed source thread pooling lib i am using (judging by the comments asking about executor service).
I use a library that provides ThreadPool capability. This is how i create a pool at the beginning of application startup.
ThreadPool fetcherPool = new ThreadPool("TASK_FETCHER", ConfigParams.minimumTaskFetcherCount, ConfigParams.maximumTaskFetcherCount, new TaskFetcher());
ThreadPool.java seems to be extending Java’s ThreadGroup class, and ThreadFetcher is as follows, which actually triggers the thread.
public class TaskFetcher extends PooledMessageWorker //Pooled Message Worker extends ReacreatableThread
public void run() {
Observer.incrementNumOfTaskFetcher();
try {
while (true) {
try {
if (Observer.isShutdowned()) {
break;
}
ControllerUtil.getInstance().waitIfPaused();
ESDRType task = (ESDRType) TaskHandler.getTask();
if (task != null) {
trigger(task);
}
} catch (Exception e) {
LogUtil.error(logger, e.getMessage(), e);
}
}
} finally {
Observer.decrementNumOfTaskFetcher();
}
}
private void trigger(ESDRType task) {
try {
GDRType[] messages = createMessages(esdr);
for (GDRType message : messages) {
RemoteDataSynchTaskProcessor remoteCDCMTaskProcessor = new RemoteDataSynchTaskProcessor(message);
remoteCDCMTaskProcessor.start(); //This thread causes a new thread appear after execution finishes
LogUtil.debug(logger, "[distributeMessage()] [Message: ", message, "]... [OK]");
}
} catch (DTSException exception) {
// Logging stuff
}
}
}
Sorry everyone, seems like it has nothing to do with that ThreadPool or RemoteDataSynchTaskProcessor.
I havent noticed another ExecutorService is created in a method that is called by ProcessResults() method in RemoteDataSynchTaskProcessor. And executor server is not shutdown after its job is down. My bad.
So if anyone has a problem like this one with unexplained thrad occurrences, check your code twice 😀 Look for the problem somewhere else 😛