简体   繁体   English

Executor服务中的从属线程-Java

[英]Dependent threads in Executor Service - Java

I am dealing with multiple threads in my program. 我正在处理程序中的多个线程。 There are some independent threads and some threads depend on the completion of some other thread before it starts execution. 有一些独立的线程,有些线程在开始执行之前取决于其他线程的完成。

Currently I'm doing this 目前我正在这样做

for ( final String inElementId : inElements ) 
{
    Thread thread = threadsMap.get( inElementId );
    if ( ( thread != null ) && thread.interrupted() ) 
    {
        Thread.currentThread().interrupt();
        throw new RuntimeException();
    } else {
        thread.join();
    }
}

So each threads checks if the thread on which it depends is still running then it waits for its completion. 因此,每个线程检查它所依赖的线程是否仍在运行,然后等待其完成。

I want to write an executor service to manage threads in a systematic way. 我想编写一个执行程序服务来系统地管理线程。 But I couldn't find a way where I can check the dependencies. 但是我找不到一种可以检查依赖关系的方法。

How can I do this using a thread pool executor service that thread B should only be submitted once thread A has completed execution ? 如何使用线程池执行程序服务执行此操作,即thread B仅在thread A完成执行后才应提交? Or is there any other better way to manage such threads? 还是有其他更好的方法来管理此类线程?

PS: It is also possible that thread B is submitted first, but it depends on A so it keeps on waiting until thread A is submitted and it completes execution. PS:也有可能首先提交thread B ,但是它依赖于A因此它将一直等待直到提交thread A并完成执行。

You can use CountDownLatch. 您可以使用CountDownLatch。

Lets say you have thread "A" that depends on thread "B". 假设您有依赖于线程“ B”的线程“ A”。

You need to create a CountDownLatch object initialize to value 1(If thread A depends on only one thread). 您需要创建一个CountDownLatch对象,将其初始化为值1(如果线程A仅依赖于一个线程)。

In thread B pass the latch object and once you complete the execution of method(method called by thread B) at the end you can keep latch.countDown(), which will count down the latch by 1. 在线程B中传递锁存器对象,并在最后完成方法(由线程B调用的方法)的执行后,可以保留latch.countDown(),它将使锁存器递减1。

Same latch object you will pass to thread A also. 您还将传递给线程A的同一闩锁对象。 But There you will be keeping latch.await() as first line, which means your thread A will wait till latch count becomes 0. 但是,您将把latch.await()保留为第一行,这意味着您的线程A将一直等到闩锁计数变为0。

So, when you thread B completes it will count down the latch and make latch count to 0. Which will trigger subsequent line in thread A, after latch.await() line. 因此,线程B完成后,它将对锁存器进行递减计数,并使锁存器计数为0。这将触发在线程A的下一行,在闩锁.await()之后。 Basically, it will trigger thread A. 基本上,它将触发线程A。

Read About Count Down latch here: http://howtodoinjava.com/core-java/multi-threading/when-to-use-countdownlatch-java-concurrency-example-tutorial/ 在此处阅读有关倒数锁存器: http : //howtodoinjava.com/core-java/multi-threading/when-to-use-countdownlatch-java-concurrency-example-tutorial/

You can also achieve the same thing using callable. 您也可以使用callable实现相同的目的。 It returns the Future instance and with that you can verify if task has been completed or not. 它返回Future实例,您可以用它来验证任务是否已完成。

Future future = executorService.submit(new Callable(){
public Object call() throws Exception {
    System.out.println("Asynchronous Callable");
    return "Callable Result";
}});

Add this future instance reference in your threads as dependency, before execution they will refer if future task is executed or not. 在您的线程中添加此将来的实例引用作为依赖项,在执行之前,它们将引用是否执行将来的任务。

if (future.isDone()){
  //do something
}

You can refer to this example - http://javahash.com/java-concurrency-future-callable-executor-example/ 您可以参考此示例-http://javahash.com/java-concurrency-future-callable-executor-example/

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

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