NIO and SQS(1)CompletableFuture Basic

本文深入探讨了Java中CompletableFuture类的功能及使用方法,包括其作为Future和CompletionStage接口实现的基础概念,以及如何通过线程池执行异步任务。此外,还介绍了CompletableFuture的各种方法,如whenComplete、thenApply等,用于处理异步操作的结果。
NIO and SQS(1)CompletableFuture Basic

Future in JDK5
ExecutorService es = Executors.newFixedThreadPool(10);
Future<Integer> f = es.submit( () -> {
//cpu usage
return 100;
});

while(!f.isDone()){
…snip...
}

Or

f.get();

f.get() will block the thread and wait for the result, while will loop the CPU.

Java 8 CompletableFuture
That is a class implement interface of Future and CompletionStage
public class CompletableFuture<T> implements Future<T>, CompletionStage<T>

It seems it is thread pool to do the things.
package com.j2c.jobssolrconsumer;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class BasicMain {

public static CompletableFuture<Integer> compute() {
final CompletableFuture<Integer> future = new CompletableFuture<>();
return future;
}

public static void main(String[] args) throws Exception {
final CompletableFuture<Integer> f = compute();

class Client extends Thread {
CompletableFuture<Integer> f;

Client(String threadName, CompletableFuture<Integer> f) {
super(threadName);
this.f = f;
}

public void run() {
try {
System.out.println(this.getName() + ": " + f.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
new Client("Client1", f).start();
new Client("Client2", f).start();
System.out.println("waiting");
f.complete(100);
// f.completeExceptionally(new Exception());
System.in.read();
}
}

Async will use Executor —> ForkJoinPool.commonPool() That is the thread pool.

public CompletableFuture<T> whenComplete()
public CompletableFuture<T> whenCompleteAsync()
public CompletableFuture<T> exceptionally()

package com.j2c.jobssolrconsumer.completablefuture;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

public class BasicMain2 {
private static Random rand = new Random();
private static long t = System.currentTimeMillis();

static int getMoreData() {
System.out.println("begin to start compute");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("end to start compute. passed " + (System.currentTimeMillis() - t) / 1000 + " seconds");
return rand.nextInt(1000);
}

public static void main(String[] args) throws Exception {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(BasicMain2::getMoreData);
future.whenComplete((v, e) -> {
System.out.println(v);
System.out.println(e);
});
System.out.println("system run here.");
// System.out.println(f.get());
System.in.read();
}
}

public <U> CompletableFuture<U> handle()
public <U> CompletableFuture<U> handleAsync()

Transform
combine some steps together

public <U> ComletableFuture<U> thenApply()
public <U> ComletableFuture<U> thenApplyAsync()

The first result will be pass to the function in the thenApplySync

Void Action
No result need to be returned

public ComletableFuture<Void> thenAccept()
public ComletableFuture<Void> thenAcceptAsync()

thenAccpetBoth will do the same thing for 2 CompletionStage

public <U> CompletableFuture<Void> thenAcceptBoth()
public <U> CompletableFuture<Void> thenAcceptBothAsync()
public CompletableFuture<Void> runAfterBoth()

Compose Stage
It will compose the previous CompletableFuture and the current CompletableFuture

public <U> CompletableFuture<U> thenCompose()
public <U> CompletableFuture<U> thenComposeAsync()

Combine Action
public <U, V> CompletableFuture<V> thenCombine()
public <U, V> CompletableFuture<V> thenCombineAsync()

Either Action

Helper Methods allOf and anyOf


References:
http://colobu.com/2016/02/29/Java-CompletableFuture/
http://www.jianshu.com/p/6f3ee90ab7d3
https://www.ibm.com/developerworks/cn/java/j-jvmc2/index.html
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值