簡體   English   中英

在Java中並行執行從屬任務

[英]Executing Dependent tasks in parallel in Java

我需要找到一種在java中並行執行任務(依賴和獨立)的方法。

  1. 任務A和任務C可以獨立運行。
  2. 任務B取決於任務A的輸出。

我檢查了java.util.concurrent Future和Fork / Join,但看起來我們無法向Task添加依賴項。

任何人都可以指出我更正Java API。

在Scala中這很容易做到,我認為你最好使用Scala。 這是我從這里開始的一個例子http://danielwestheide.com/ (新手的Scala指南第16部分:從哪里開始)這個人有一個很棒的博客(我不是那個人)

讓我們一起喝咖啡吧。 要做的任務是:

  1. 研磨所需的咖啡豆(沒有前面的任務)
  2. 加熱一些水(沒有先前的任務)
  3. 使用研磨咖啡和熱水沖泡濃縮咖啡(取決於1和2)
  4. 泡一些牛奶(沒有先前的任務)
  5. 將泡沫牛奶和濃縮咖啡混合在一起(取決於3,4)

或作為一棵樹:

Grind   _
Coffe    \
          \   
Heat    ___\_Brew____ 
Water                \_____Combine
                     /
Foam    ____________/
Milk

在使用並發api的java中,這將是:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Barrista {

    static class HeatWater implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("Heating Water");
            Thread.sleep(1000);
            return "hot water";
        }
    }

    static class GrindBeans implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("Grinding Beans");
            Thread.sleep(2000);
            return "grinded beans";
        }
    }

    static class Brew implements Callable<String> {

        final Future<String> grindedBeans;
        final Future<String> hotWater;

        public Brew(Future<String> grindedBeans, Future<String> hotWater) {
            this.grindedBeans = grindedBeans;
            this.hotWater = hotWater;
        }

        @Override
        public String call() throws Exception
        {
            System.out.println("brewing coffee with " + grindedBeans.get()
                    + " and " + hotWater.get());
            Thread.sleep(1000);
            return "brewed coffee";
        }
    }

    static class FrothMilk implements Callable<String> {

        @Override
        public String call() throws Exception {
            Thread.sleep(1000);
            return "some milk";
        }
    }

    static class Combine implements Callable<String> {

        public Combine(Future<String> frothedMilk, Future<String> brewedCoffee) {
            super();
            this.frothedMilk = frothedMilk;
            this.brewedCoffee = brewedCoffee;
        }

        final Future<String> frothedMilk;
        final Future<String> brewedCoffee;

        @Override
        public String call() throws Exception {
            Thread.sleep(1000);
            System.out.println("Combining " + frothedMilk.get() + " "
                    + brewedCoffee.get());
            return "Final Coffee";
        }

    }

    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(2);

        FutureTask<String> heatWaterFuture = new FutureTask<String>(new HeatWater());
        FutureTask<String> grindBeans = new FutureTask<String>(new GrindBeans());
        FutureTask<String> brewCoffee = new FutureTask<String>(new Brew(grindBeans, heatWaterFuture));
        FutureTask<String> frothMilk = new FutureTask<String>(new FrothMilk());
        FutureTask<String> combineCoffee = new FutureTask<String>(new Combine(frothMilk, brewCoffee));

        executor.execute(heatWaterFuture);
        executor.execute(grindBeans);
        executor.execute(brewCoffee);
        executor.execute(frothMilk);
        executor.execute(combineCoffee);


        try {

            /**
             *  Warning this code is blocking !!!!!!!
             */         
            System.out.println(combineCoffee.get(20, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            System.out.println("20 SECONDS FOR A COFFEE !!!! I am !@#! leaving!!");
            e.printStackTrace();
        } finally{
                executor.shutdown();
            }
        }
    }

確保您添加超時以確保您的代碼不會永遠等待完成某些事情,這可以通過使用Future.get(long,TimeUnit)完成,然后相應地處理失敗。

它在scala中更好,但是,它就像在博客上一樣:准備一些咖啡的代碼看起來像這樣:

def prepareCappuccino(): Try[Cappuccino] = for {
  ground <- Try(grind("arabica beans"))
  water <- Try(heatWater(Water(25)))
  espresso <- Try(brew(ground, water))
  foam <- Try(frothMilk("milk"))
} yield combine(espresso, foam)

所有方法返回未來(鍵入的未來),例如grind將是這樣的:

def grind(beans: CoffeeBeans): Future[GroundCoffee] = Future {
   // grinding function contents
}

對於所有實現,請查看博客,但這就是它的全部內容。 您也可以輕松集成Scala和Java。 我真的建議在Scala而不是Java中做這種事情。 Scala需要更少的代碼,更清晰和事件驅動。

具有依賴性的任務的通用編程模型是Dataflow 簡化模型,其中每個任務只有一個,但重復的依賴是Actor模型 Java有很多actor庫,但數據流很少。 另請參見: which-actor-model-library-framework-for-javajava-pattern-for-nested-callbacks

使用BlockingQueue。 將任務A的輸出放入隊列,任務B阻塞,直到隊列中有可用的東西。

這些文檔包含實現此目的的示例代碼: http//docs.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html

你需要的是CountDownLatch

final CountDownLatch gate = new CountDownLatch(2);
// thread a
new Thread() {
    public void run() {
        // process
        gate.countDown();
    }
}.start();

// thread c
new Thread() {
    public void run() {
        // process
        gate.countDown();
    }
}.start();

new Thread() {
    public void run() {
        try {
            gate.await();
            // both thread a and thread c have completed
            // process thread b
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}.start();

作為替代方案,根據您的方案,您也可以使用BlockingQueue來實現Producer-Consumer模式。 請參閱文檔頁面上的示例。

如果任務B依賴於任務A的輸出,我首先會質疑任務B是否真的是一個單獨的任務。 如果存在以下情況,則分離任務是有意義的:

  • 任務B在需要任務A的結果之前可以完成的一些非常重要的工作量
  • 任務B是一個長期持續的過程,處理來自任務A的許多不同實例的輸出
  • 還有一些其他任務(比如D)也使用任務A的結果

假設它是一個單獨的任務,那么您可以允許任務A和B共享BlockingQueue ,以便任務A可以傳遞任務B數據。

使用此庫https://github.com/familysyan/TaskOrchestration 它為您管理任務依賴。

有一個專門用於此目的的java庫(免責聲明:我是這個庫的所有者),名為Dexecutor

以下是如何實現所需結果的方法,您可以在此處閱讀更多相關信息

@Test
public void testDependentTaskExecution() {

    DefaultDependentTasksExecutor<String, String> executor = newTaskExecutor();

    executor.addDependency("A", "B");
    executor.addIndependent("C");

    executor.execute(ExecutionBehavior.RETRY_ONCE_TERMINATING);

}

private DefaultDependentTasksExecutor<String, String> newTaskExecutor() {
    return new DefaultDependentTasksExecutor<String, String>(newExecutor(), new SleepyTaskProvider());
}

private ExecutorService newExecutor() {
    return Executors.newFixedThreadPool(ThreadPoolUtil.ioIntesivePoolSize());
}

private static class SleepyTaskProvider implements TaskProvider<String, String> {

    public Task<String, String> provid(final String id) {

        return new Task<String, String>() {

            @Override
            public String execute() {
                try {
                    //Perform some task
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String result = id + "processed";
                return result;
            }

            @Override
            public boolean shouldExecute(ExecutionResults<String, String> parentResults) {
                ExecutionResult<String, String> firstParentResult = parentResults.getFirst();
                //Do some logic with parent result
                if ("B".equals(id) && firstParentResult.isSkipped()) {
                    return false;
                }
                return true;
            }
        };          
    }

}

Java定義了一個CompletableFuture類。

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html

這就是你要找的東西。 它有助於構建執行流程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM