如何调整3个线程的控制流量?

时间:2022-04-18 21:01:08
// Task 1 -- the main thread
        SimilarityResponse w2vResponse = questionClassifier.compute(questionInfo);

        // Task 2
        String sku = questionInfo.getSku();
        String question = questionInfo.getQuestion();
        Callable<ResponseList> dssmTask = () -> this.dssmCompute(sku, question);
        Future<ResponseList> dssmService = executorService.submit(dssmTask);
        ResponseList dssmResponse;
        try {
            LOGGER.info("start dssm ... {} ", question);
            dssmResponse = dssmService.get(Parameters.getParserTimeLimit(), TimeUnit.MILLISECONDS);
            LOGGER.info("dssmResponse ... {} ", dssmResponse);
        } catch (ExecutionException | InterruptedException e) {
            LOGGER.warn("ExecutionException | InterruptedException");
            e.printStackTrace();
        } catch (TimeoutException te) {
            dssmService.cancel(true);
            LOGGER.warn("DSSM time out for {} {}", sku, question);
        }

        // Task 3
        Callable<ResponseList> stsTask = () -> this.stsCompute(sku, question);
        Future<ResponseList> stsService = executorService.submit(stsTask);
        ResponseList stsResponse;
        try {
            LOGGER.info("start sts ... {} ", question);
            stsResponse = stsService.get(Parameters.getParserTimeLimit(), TimeUnit.MILLISECONDS);
            LOGGER.info("stsResponse ... {} ", stsResponse);
        } catch (ExecutionException | InterruptedException e) {
            LOGGER.warn("ExecutionException | InterruptedException");
            e.printStackTrace();
        } catch (TimeoutException te) {
            stsService.cancel(true);
            LOGGER.warn("STS time out for {} {}", sku, question);
        }

        // Last step == do something for above
        SimilarityResponse ensemble = new SimilarityResponse();
        return ensemble;

Before executing the last step, how can I make sure the Task 1-3 are already completed? The current code seems it finishes Task 1 first and then directly returns.

在执行最后一步之前,如何确保任务1-3已经完成?当前代码似乎首先完成任务1然后直接返回。

1 个解决方案

#1


2  

You should use CountDownLatch. Create instance of it in your main thread and pass this instance to your tasks (Callables). Then when task is finished call latch.countDown(). In the last step of your code call latch.await() to wait for each task to finish. It would look something like this (if your callables were created as lambdas):

您应该使用CountDownLatch。在主线程中创建它的实例,并将此实例传递给您的任务(Callables)。然后当任务完成时调用latch.countDown()。在代码的最后一步调用latch.await()来等待每个任务完成。它看起来像这样(如果你的callables被创建为lambdas):

final CountDownLatch latch = new CountDownLatch(3);

for(int x = 0; x < 3; x++) {
    service.submit( () -> {
        // do something
        latch.countDown();
    });
}

// in the end wait for tasks to finish
latch.await();

#1


2  

You should use CountDownLatch. Create instance of it in your main thread and pass this instance to your tasks (Callables). Then when task is finished call latch.countDown(). In the last step of your code call latch.await() to wait for each task to finish. It would look something like this (if your callables were created as lambdas):

您应该使用CountDownLatch。在主线程中创建它的实例,并将此实例传递给您的任务(Callables)。然后当任务完成时调用latch.countDown()。在代码的最后一步调用latch.await()来等待每个任务完成。它看起来像这样(如果你的callables被创建为lambdas):

final CountDownLatch latch = new CountDownLatch(3);

for(int x = 0; x < 3; x++) {
    service.submit( () -> {
        // do something
        latch.countDown();
    });
}

// in the end wait for tasks to finish
latch.await();