fix:异步练习

上级 87c6210c
package com.kwan.shuyu.heima.netty_05_future;
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.Callable;
/**
* netty Future 测试
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/26 21:07
*/
@Slf4j
public class TestNettyFuture {
public static void main(String[] args) throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup();
EventLoop eventLoop = group.next();
Future<Integer> future = eventLoop.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
log.info("执行计算");
Thread.sleep(1000);
return 78;
}
});
//3.主线程通过future 来获取结果
log.info("等待处理结果");
log.info("处理结果是={}", future.get());
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_05_future;
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.concurrent.Callable;
/**
* netty Future 测试
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/26 21:07
*/
@Slf4j
public class TestNettyFutureListener {
public static void main(String[] args) throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup();
EventLoop eventLoop = group.next();
Future<Integer> future = eventLoop.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
log.info("执行计算");
Thread.sleep(1000);
return 78;
}
});
//异步接受数据
future.addListener(new GenericFutureListener<Future<? super Integer>>() {
@Override
public void operationComplete(Future<? super Integer> future) throws Exception {
//3.主线程通过future 来获取结果
log.info("等待处理结果");
log.info("处理结果是={}", future.getNow());
}
});
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_05_future;
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;
/**
* netty promise 测试 成功和失败处理
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/26 21:07
*/
@Slf4j
public class TestNettyPromiseFailure {
public static void main(String[] args) throws Exception {
EventLoop eventLoop = new NioEventLoopGroup().next();
final DefaultPromise promise = new DefaultPromise(eventLoop);
new Thread(() -> {
log.info("开始计算....");
try {
// int num = 1 / 0;
Thread.sleep(1000);
promise.setSuccess(20);
} catch (InterruptedException e) {
e.printStackTrace();
promise.setFailure(e);
}
}).start();
final Throwable cause = promise.cause();
if (cause != null) {
//3.主线程通过future 来获取结果
log.info("等待处理结果");
log.info("处理结果是={}", cause.getMessage());
} else {
//3.主线程通过future 来获取结果
log.info("等待处理结果");
log.info("处理结果是={}", promise.getNow());
}
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_05_future;
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;
/**
* netty promise 测试 成功
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/26 21:07
*/
@Slf4j
public class TestNettyPromiseSuccess {
public static void main(String[] args) throws Exception {
EventLoop eventLoop = new NioEventLoopGroup().next();
final DefaultPromise promise = new DefaultPromise(eventLoop);
new Thread(() -> {
log.info("开始计算....");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
promise.setSuccess(20);
}).start();
//3.主线程通过future 来获取结果
log.info("等待处理结果");
log.info("处理结果是={}", promise.get());
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册