Rohit Bajaj
asked on
Threads scheduled with ThreadPoolTaskExecutor keep on running
Hi,
I have the following file :
There is the following piece of code in another file which creates threads :
The SearchTask.java file is as follows :
There are some threads created by this java application. But when the tasks are completed i see the threads still running in my debug panel and the program also doesnot exits.
How can i exit the program properly once the tasks are done and why the threads are in running mode even though the code part is over ?
Thanks
I have the following file :
package com.yatra.extremesearch.app;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yatra.extremesearch.job.ExtremeSearchJob;
public class ExtremeSearchApp {
private static final Logger logger = Logger.getLogger(ExtremeSearchApp.class);
public static void main(String args[]){
try {
logger.info("=====Starting Extreme search======");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring/root-context.xml");
ExtremeSearchJob eJob = (ExtremeSearchJob) context.getBean("extremeSearchJob");
eJob.execute();
} catch (Exception e) {
logger.error(e.getMessage());
}
}
}
There is the following piece of code in another file which creates threads :
@Autowired @Qualifier("yatraExecutorService") ThreadPoolTaskExecutor yatraExecutorService;
CountDownLatch countDownLatch = new CountDownLatch(params.size());
for (ExtremeSearchParam param : params) {
SearchTask task = new SearchTask(this,param,parentFolder,countDownLatch);
yatraExecutorService.submit(task);
}
The SearchTask.java file is as follows :
package com.yatra.extremesearch.service;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import com.yatra.extremesearch.model.ExtremeSearchParam;
@SuppressWarnings("rawtypes")
public class SearchTask implements Callable{
ExtremeSearchParam param;
SearchParamService searchParamService;
String parentFolder;
CountDownLatch countDownLatch;
public SearchTask(SearchParamService searchParamService, ExtremeSearchParam param, String parentFolder, CountDownLatch countDownLatch) {
this.param=param;
this.searchParamService=searchParamService;
this.parentFolder=parentFolder;
this.countDownLatch = countDownLatch;
}
@Override
public Object call() throws Exception {
try{
searchParamService.processExtremeSearchResp(param,parentFolder);
return null;
}finally{
countDownLatch.countDown();
}
}
}
There are some threads created by this java application. But when the tasks are completed i see the threads still running in my debug panel and the program also doesnot exits.
How can i exit the program properly once the tasks are done and why the threads are in running mode even though the code part is over ?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The main problem is that the app is not exiting. The 'interest problem' is that threads are 'running' but not engaged in work tasks. Both problems are created by not shutting down the thread pool executor.
>> The 'interest problem' is that threads are 'running' but not engaged in work tasks.
You cannot be sure about that "not engaged in work tasks". There could be a deadlock between threads, or if the search is performed in DB, if there is some problem with connections that will keep threads running.
but most probably you are right - the author has not "shutdown()" the pool. Only he knows that :-) Hey Robinsuri, put this lines:
for (;;) {
int count = yatraExecutorService.getAc tiveCount( );
System.out.println("Active Threads : " + count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count == 0) {
yatraExecutorService.shutd own();
break;
}
}
after this lines:
for (ExtremeSearchParam param : params) {
SearchTask task = new SearchTask(this,param,pare ntFolder,c ountDownLa tch);
yatraExecutorService.submi t(task);
}
just to check if this is the case?!
You cannot be sure about that "not engaged in work tasks". There could be a deadlock between threads, or if the search is performed in DB, if there is some problem with connections that will keep threads running.
but most probably you are right - the author has not "shutdown()" the pool. Only he knows that :-) Hey Robinsuri, put this lines:
for (;;) {
int count = yatraExecutorService.getAc
System.out.println("Active
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count == 0) {
yatraExecutorService.shutd
break;
}
}
after this lines:
for (ExtremeSearchParam param : params) {
SearchTask task = new SearchTask(this,param,pare
yatraExecutorService.submi
}
just to check if this is the case?!
You cannot be sure about that "not engaged in work tasks".I'm going by what Robinsuri says. Deadlock is possible but unlikely - that would be the sign of a 'broken' thread pool
:)
public class SearchTask implements Callable<String> {
and the method in this way:
@Override
public Object call() throws Exception {
try {
searchParamService.process
} finally {
countDownLatch.countDown()
}
System.out.println("finish
return "finished";
}
if you get string "finished" in console as many times as your threads are then they must complete.
Also I cannot see where is "await" method of your CountDownLatch?