我需要找到一种在java中并行执行任务(依赖和独立)的方法。
我检查了java.util.concurrent Future和Fork / Join,但看起来我们无法向任务添加依赖。
任何人都可以指出我更正Java API。
答案 0 :(得分:10)
在Scala中,这很容易做到,我认为你最好使用Scala。这是我从这里提取的一个例子http://danielwestheide.com/(新手指南Scala第16部分:从何处开始)这个人有一个很棒的博客(我不是那个人)
让我们一起喝咖啡吧。要做的任务是:
或作为一棵树:
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需要更少的代码,更清洁和事件驱动。
答案 1 :(得分:3)
具有依赖关系的任务的通用编程模型是Dataflow。简化模型,其中每个任务只有一个但重复的依赖是Actor model。 Java有很多actor库,但数据流很少。 另请参阅:which-actor-model-library-framework-for-java,java-pattern-for-nested-callbacks
答案 2 :(得分:1)
使用BlockingQueue。将任务A的输出放入队列,任务B阻塞,直到队列中有可用的东西。
文档包含实现此目的的示例代码:http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html
答案 3 :(得分:0)
您需要的是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模式。请参阅文档页面上的示例。
答案 4 :(得分:0)
如果任务B依赖于任务A的输出,我首先会质疑任务B是否真的是一个单独的任务。如果有:
,则分离任务是有意义的假设这是一项单独的任务,那么你可以允许任务A&amp; B共享BlockingQueue
,以便任务A可以传递任务B数据。
答案 5 :(得分:0)
使用此库https://github.com/familysyan/TaskOrchestration。它为您管理任务依赖。
答案 6 :(得分:0)
有一个专门用于此目的的java库(免责声明:我是这个库的所有者),名为Dexecutor
以下是如何达到预期效果的结果,您可以阅读更多相关信息here
@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;
}
};
}
}
答案 7 :(得分:0)
Java定义了一个CompletableFuture类。
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html
这就是您要寻找的。 它有助于建立执行流程。