繁体   English   中英

Java多线程问题

[英]Java Multithread Issue

在下面的代码中,我的主要任务不是等待子任务完成其执行。 我是Java线程的新手。 所以我无法修复它。 我用谷歌搜索,没有发现运气。 请帮助我解决此线程问题。 码:

class ExecutorServiceManager{
public static ExecutorService getExecutor() {
    if (executorService == null) {
        try {
            lock.lock();
            if (executorService == null) {
                executorService = Executors.newFixedThreadPool(150);
            }
        } finally {
            lock.unlock();
        }
    }

    if(executorService instanceof ThreadPoolExecutor) {
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService;
        int corePoolSize = threadPoolExecutor.getCorePoolSize();
        int maximumPoolSize = threadPoolExecutor.getMaximumPoolSize();
        Logger.info(ExecutorServiceManager.class, "ExecutorInfo: CorePoolSize:%s, MaxPoolSize:%s", corePoolSize, maximumPoolSize);
    }
    return executorService;
}}

class ServiceImpl{
ExecutorServiceManager executorServiceManager;
private void processConversion(String category, Map<String, String> couchDeltaMap, String processKey, String reqId) {
    try {
        ProgressVo progressVo = new ProgressVo();
        CountDownLatch pgCntxtcountDownLatch = new CountDownLatch(1);
        executorServiceManager.getExecutor().submit(new MainTask(category, processKey, pgCntxtcountDownLatch, executorServiceManager, progressVo));
        Logger.info(ServiceImpl.class, "ExecutorInfo: CorePoolSize:%s, MaxPoolSize:%s", corePoolSize, maximumPoolSize);
        pgCntxtcountDownLatch.await();
    } catch(InterruptedException ie) {} 
      catch(Exception ex) {}
}}

class MainTask implements Runnable{
@Override
public void run() {     
    executorService = executorServiceManager.getExecutor();
    executorService.submit(new SubTask(progressVo, couchDeltaMap, reqId, executorServiceManager));

    //I want the below operation to be executed, if and only the subtask completed its execution. 
    //But the below logger is printing before the subtask completed its execution.      
    Logger.info(MainTask.class, "It got executed before the subtask completed its processing");
    pgCntxtcountDownLatch.countDown();
}}

class SubTask implements Runnable{
@Override
public void run() {     
    executorService = executorServiceManager.getExecutor();
    doSomeProcess;
    //It stopped in the middle, and the Main task started executing the remaining operation
}}

为了让您的主要任务等待子任务执行,您可以通过以下方式使用Executor.submit()返回的Future

class MainTask implements Runnable{
@Override
public void run() {     
    executorService = executorServiceManager.getExecutor();
    Future subTask = executorService.submit(new SubTask(progressVo, couchDeltaMap, reqId, executorServiceManager));
    try{
      subTask.get(); //wait for completion of the subtask
    } catch(Exception e){
         //You probably want better exception catching, this is just an example
    }

    Logger.info(MainTask.class, "It got executed before the subtask completed its processing");
    pgCntxtcountDownLatch.countDown();
}}
class MainTask implements Runnable{
@Override
public void run() { 
executorService = manager.getExecutor();
List<Future<Runnable>> futures = new ArrayList<Future<Runnable>>();
while (!pageCntxts.isEmpty()) {
    popped = pageCntxts.pop();
    Future future = executorService.submit(new SubTask(progressVo, couchDeltaMap, reqId,manager));
    futures.add(future);
    if(pageCntxts.isEmpty())
        loadPageCntxtWithNext25Records(progressVo);
    processNum++;
}
Logger.debug(MainTask.class, "Internal Thread Running Starts with data size: "+futures.size());
for (Future<Runnable> future : futures) {
   future.get();
}
Logger.debug(MainTask.class, "Internal Thread Running Ends");}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM