目录
目标
Netty版本
Netty官方API
三者之间的关系
基本使用方法
java.util.concurrent.Future
io.netty.util.concurrent.Future
io.netty.util.concurrent.Promise
目标
- 了解io.netty.util.concurrent.Promise与io.netty.util.concurrent.Future的基本使用方法。
- 了解java.util.concurrent.Future、io.netty.util.concurrent.Promise,io.netty.util.concurrent.Future之间的关系。
Netty版本
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.87.Final</version>
</dependency>
Netty官方API
Netty API Reference (4.1.89.Final)https://netty.io/4.1/api/index.html
三者之间的关系
区别
- jdk自带的Future只能同步等待结果。
- netty自带的Future能同步等待结果,也可以用异步的方式(如:使用addListener方法设置回调方法)等待结果。
- Promise有Future的所有功能,脱离任务独立存在(可以主动创建并赋结果),只作为线程之间传递结果的容器。
关联
Promise extends netty自带的Future extends jdk自带的Future
基本使用方法
java.util.concurrent.Future
package com.ctx.netty;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;
@Slf4j
public class JavaFuture {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future = executor.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(1000);
return "result";
}
});
//同步阻塞返回结果。
try {
log.info("返回结果值:{}",future.get());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
}
io.netty.util.concurrent.Future
package com.ctx.netty;
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
@Slf4j
public class NettyFuture {
public static void main(String[] args) {
NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
EventLoop eventLoop = eventExecutors.next();
Future<Map<String, Object>> future = eventLoop.submit(new Callable<Map<String, Object>>() {
@Override
public Map<String, Object> call() throws Exception {
Thread.sleep(1000);
Map<String, Object> map = new HashMap<>();
map.put("name", "zhangsan");
return map;
}
});
new NettyFuture().getNow(future);
}
/**
* 同步阻塞等待结果。
* @param future
*/
public void get(Future<Map<String, Object>> future){
try {
log.info("结果是:"+future.get());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
/**
*异步方式等待结果。
* @param future
*/
public void getNow(Future<Map<String, Object>> future){
future.addListener(new GenericFutureListener<Future<? super Map<String, Object>>>() {
//拿到结果以后回调方法。所以此时执行get()还是getNow()是一样的效果。
@Override
public void operationComplete(Future<? super Map<String, Object>> future) throws Exception {
log.info("结果是:"+future.getNow());
}
});
}
}
io.netty.util.concurrent.Promise
package com.ctx.netty;
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Promise;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
@Slf4j
public class NettyPromise {
public static void main(String[] args) {
NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
EventLoop eventLoop = eventExecutors.next();
//自定义类型,用于填充结果。
Promise<Map<String, Object>> promise = new DefaultPromise<>(eventLoop);
new Thread(()->{
try {
Thread.sleep(1000);
Map<String, Object> map = new HashMap<>();
map.put("name","zhangsan");
promise.setSuccess(map);
} catch (Exception e) {
promise.setFailure(e);
}
}).start();
try {
Map<String, Object> map = promise.get();
map.forEach((k,v)->{
System.out.println(k+"="+v);
});
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
}