java如何实现任务依赖,如何在没有任何阻塞的情况下在Java 8中执行依赖任务

我以为我会自己回答这个问题,可以在java中使用CompletableFutures而不是Futures. CompletableFutures允许通过thenCombine方法进行合成,该方法类似于scalas flatMap.现在没有阻塞发生,只需要3个线程即可实现最快的时间.

import java.util.concurrent.CompletableFuture;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.function.BiFunction;

import java.util.function.Supplier;

public class Barrista

{

// number of threads used in executor

static final int NOTHREADS = 3;

// time of each task

static final int HEATWATER = 1000;

static final int GRINDBEANS = 1000;

static final int FROTHMILK = 1000;

static final int BREWING = 1000;

static final int COMBINE = 1000;

// method to simulate work (pause current thread without throwing checked exception)

public static void pause(long t)

{

try

{

Thread.sleep(t);

}

catch(Exception e)

{

throw new Error(e.toString());

}

}

// task to heat some water

static class HeatWater implements Supplier

{

@Override

public String get()

{

System.out.println("Heating Water");

pause(HEATWATER);

return "hot water";

}

}

// task to grind some beans

static class GrindBeans implements Supplier

{

@Override

public String get()

{

System.out.println("Grinding Beans");

pause(GRINDBEANS);

return "grinded beans";

}

}

// task to froth some milk

static class FrothMilk implements Supplier

{

@Override

public String get()

{

System.out.println("Frothing some milk");

pause(FROTHMILK);

return "some milk";

}

}

// task to brew some coffee

static class Brew implements BiFunction

{

@Override

public String apply(String groundBeans, String heatedWater)

{

System.out.println("Brewing coffee with " + groundBeans + " and " + heatedWater);

pause(BREWING);

return "brewed coffee";

}

}

// task to combine brewed coffee and milk

static class Combine implements BiFunction

{

@Override

public String apply(String frothedMilk, String brewedCoffee)

{

System.out.println("Combining " + frothedMilk + " "+ brewedCoffee);

pause(COMBINE);

return "Final Coffee";

}

}

public static void main(String[] args)

{

ExecutorService executor = Executors.newFixedThreadPool(NOTHREADS);

long startTime = System.currentTimeMillis();

try

{

// create all the tasks and let the executor handle the execution order

CompletableFuture frothMilk = CompletableFuture.supplyAsync(new FrothMilk(), executor);

CompletableFuture heatWaterFuture = CompletableFuture.supplyAsync(new HeatWater(), executor);

CompletableFuture grindBeans = CompletableFuture.supplyAsync(new GrindBeans(), executor);

CompletableFuture brew = heatWaterFuture.thenCombine(grindBeans, new Brew());

CompletableFuture coffee = brew.thenCombine(frothMilk, new Combine());

// final coffee

System.out.println("Here is the coffee:" + coffee.get());

// analyzing times:

System.out.println("\n\n");

System.out.println("Actual time: \t\t\t\t" + (System.currentTimeMillis() - startTime)/1000.0);

// compute the quickest possible time:

long path1 = Math.max(GRINDBEANS, HEATWATER)+ BREWING + COMBINE;

long path2 = FROTHMILK + COMBINE;

System.out.println("Quickest time multi-threaded:\t\t" + Math.max(path1, path2)/1000.0);

// compute the longest possible time:

long longestTime = HEATWATER + GRINDBEANS + FROTHMILK + BREWING + COMBINE;

System.out.println("Quickest time single-threaded thread:\t" + longestTime/1000.0);

}

catch (Exception e)

{

e.printStackTrace();

}

finally

{

executor.shutdown();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值