future
在java中结合线程池,返回Callable返回任务使用
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(2);
Future<Integer> future = executorService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
log.debug("执行计算");
Thread.sleep(1000);
return 50;
}
});
log.debug("等待结果");
log.debug("结果是{}",future.get());
}
在netty中结合EventLoop进行一步操作等待
public static void main(String[] args) throws ExecutionException, InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup();
EventLoop eventLoop = group.next();
Future<Object> future = eventLoop.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
log.debug("执行计算");
return 70;
}
});
//1.同步获取结果
// log.debug("等待结果");
// //同步阻塞获取结果
// log.debug("等待结果是{}",future.get());
//2.异步获取结果
future.addListener(new GenericFutureListener<Future<? super Object>>() {
@Override
public void operationComplete(Future<? super Object> future) throws Exception {
boolean success = future.isSuccess();
log.debug("是否成功;{}", success);
log.debug("等待结果是{}", future.getNow());
}
});
}
与
DefaultPromise集合使用 操作结果的设置 协调多个异步操作
public static void main(String[] args) throws ExecutionException, InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup();
EventLoop eventLoop = group.next();
DefaultPromise<Integer> promise = new DefaultPromise<>(eventLoop);
new Thread(() -> {
log.debug("开始计算。。。。");
try {
// int i = 1 / 0;
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
promise.setFailure(e);
}
promise.setSuccess(80);
}).start();
log.debug("等待结果");
log.debug("等待结果是{}", promise.get());
promise.addListener(future -> {
if (future.isDone()) {
if (future.isCancelled()) {
System.out.println("失败了");
}
if (future.isSuccess()) {
Object object = future.get();
System.out.println(">>>>>>>>>>>>>>:"+object);
}
}
});
}
事件通知与回调机制
public static void main(String[] args) throws InterruptedException {
new ServerBootstrap()
.group(new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf byteBuf = msg instanceof ByteBuf ? ((ByteBuf) msg) : null;
ChannelPromise channelPromise = ctx.newPromise();
String constantStr = byteBuf.toString(Charset.defaultCharset());
byteBuf.release();//思考需要释放ByteBuf 在这里释放一次就好
ByteBuf response = ctx.alloc().buffer(20);
switch (constantStr) {
case "1":
response.writeBytes("牛逼".getBytes());
ctx.writeAndFlush(response, channelPromise);//需要释放 response吗?
break;
case "2":
// byteBuf.release();//思考需要释放ByteBuf 不可重复释放
response.writeBytes("牛顿".getBytes());
ctx.writeAndFlush(response, channelPromise);//需要释放 response吗?
break;
case "3":
// byteBuf.release();//思考需要释放ByteBuf 不可重复释放
response.writeBytes("牛爸".getBytes());
ctx.writeAndFlush(response, channelPromise);//需要释放 response吗?
break;
default:
System.out.println(">>>>>>>>:" + byteBuf.toString(Charset.defaultCharset()));
// byteBuf.release();//思考需要释放ByteBuf 不可重复释放
response.writeBytes("如果帅有罪,请把我埋葬!".getBytes());
ctx.writeAndFlush(response, channelPromise);//需要释放 response吗?
}
//等待做完
if (channelPromise.isDone() && channelPromise.isSuccess()) {
System.out.println("《《《写入完成》》》");
}
}
});
}
}).bind(new InetSocketAddress(8080));
}
本文来自博客园,作者:余生请多指教ANT,转载请注明原文链接:https://www.cnblogs.com/wangbiaohistory/p/18704331

浙公网安备 33010602011771号