java - 在 Java 中并行执行依赖任务

标签 java multithreading

我需要找到一种在 java 中并行执行任务(依赖和独立)的方法。

  1. 任务 A 和任务 C 可以独立运行。
  2. 任务 B 取决于任务 A 的输出。

我检查了 java.util.concurrent Future 和 Fork/Join,但看起来我们无法将依赖项添加到任务。

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

最佳答案

在 Scala 中这很容易做到,我认为你最好使用 Scala。这是我从这里提取的示例 http://danielwestheide.com/ (Scala 新手指南第 16 部分:从这里到哪里去)这个人有一个很棒的博客(我不是那个人)

让我们以一位 barrista 煮咖啡为例。要做的任务是:

  1. 研磨所需的咖啡 bean (无前置任务)
  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)

所有方法都返回一个 future (类型化的 future ),例如 grind 会是这样的:

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

有关所有实现,请查看博客,但仅此而已。您也可以轻松集成 Scala 和 Java。我真的建议在 Scala 而不是 Java 中做这种事情。 Scala 需要更少的代码,更简洁和事件驱动。

关于java - 在 Java 中并行执行依赖任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10855045/

相关文章:

java - jdbc 连接错误 : not associated with a managed connection

c++ - 如何创建一个新线程并在一段时间后终止它?

c - 我需要了解编译器如何解释 &tcb -> context

python - 在 Python 中使用 Multiprocessing+Threads 加速应用程序时遇到问题

java - Spring 不使用 mongo 自定义转换器

java - 设置 int 值会导致性能问题

java - 如何在不使用 ids 的情况下从 View 中获取值?

java - 如何将 outputStream 转换为字节数组?

c# - 在写入文件时复制文件线程安全吗?

java - 为什么支持共享获取的同步器是独占获取的?