2017-03-03 83 views
0

在下面的代码中,我的主要任务是不等待子任务完成其执行。我是Java Thread的新手。所以我无法修复它。我谷歌,发现没有运气。请帮我解决这个线程问题。 代码: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 
}} 
+1

双检锁坏了,除非你采取某些步骤,这是不必要的,因为不管怎样都不应该使用双重检查锁定。你有没有采取措施确保不必要的双重检查锁定习惯用法能正常工作? –

+0

如果你是java线程的新手,我会坚持使用基本的线程类。它会迫使你理解机制,然后你可以使用更容易使用的类,如果理解机制 – efekctive

回答

0

为了让你的主要任务等待子任务的执行,你可以使用由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(); 
}} 
+0

感谢您的代码。我试着像你所说的那样。请参阅下面的代码。即使使用此代码,我在控制台中也看不到记录器“内部线程正在运行结束”消息。 – Ismail

+0

我无法在此评论框中添加代码。因此我把它放在答案部分。请参阅该代码。即使使用此代码,我在控制台中也看不到记录器“内部线程正在运行结束”消息。而我正在看MainTask记录器,并说它已完成处理。 – Ismail

0
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");}}