学一遍根本记不住,那就再学一遍
public static void test_nettyFuture() {NioEventLoopGroup group = new NioEventLoopGroup();log.info("开始提交任务");Future<String> future = group.next().submit(() -> {log.info("执行异步任务...");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}log.info("异步任务执行完成...");return "ok";});log.info("任务提交结束");future.addListener(new GenericFutureListener<Future<String>>() {@Overridepublic void operationComplete(Future<String> future) throws Exception {String s = (String) future.get();log.info("监听任务完成回调.......{}", s);}});log.info("任务添加监听结束");
}
public static void test_nettyPromise() {NioEventLoopGroup group = new NioEventLoopGroup(10);// 指定谁去通知回调方法, 即哪1个线程去调用operationComplete方法DefaultPromise<String> promise = new DefaultPromise<>(group.next());log.info("提交任务开始");group.next().submit(() -> {try {log.info("开始执行异步任务...");TimeUnit.SECONDS.sleep(5);promise.setSuccess("ojdk");log.info("异步任务还须再执行会儿...");TimeUnit.SECONDS.sleep(5);log.info("异步任务执行完毕...");return "ok";} catch (Exception e) {log.info("异步任务执行发生异常...");promise.setFailure(e);return "ok";}});log.info("提交任务结束");log.info("添加回调开始");promise.addListener(new GenericFutureListener<Future<String>>() {@Overridepublic void operationComplete(Future<String> future) throws Exception {log.info("回调获取到异步结果开始");String result = future.get();log.info("回调获取到异步结果: {}", result);}});log.info("添加回调结束");}