Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
netty-demo
提交
7971cb27
N
netty-demo
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
netty-demo
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
netty-demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
7971cb27
编写于
4月 26, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:异步练习
上级
87c6210c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
150 addition
and
0 deletion
+150
-0
src/main/java/com/kwan/shuyu/heima/netty_05_future/TestNettyFuture.java
...com/kwan/shuyu/heima/netty_05_future/TestNettyFuture.java
+34
-0
src/main/java/com/kwan/shuyu/heima/netty_05_future/TestNettyFutureListener.java
.../shuyu/heima/netty_05_future/TestNettyFutureListener.java
+41
-0
src/main/java/com/kwan/shuyu/heima/netty_05_future/TestNettyPromiseFailure.java
.../shuyu/heima/netty_05_future/TestNettyPromiseFailure.java
+42
-0
src/main/java/com/kwan/shuyu/heima/netty_05_future/TestNettyPromiseSuccess.java
.../shuyu/heima/netty_05_future/TestNettyPromiseSuccess.java
+33
-0
未找到文件。
src/main/java/com/kwan/shuyu/heima/netty_05_future/TestNettyFuture.java
0 → 100644
浏览文件 @
7971cb27
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
src/main/java/com/kwan/shuyu/heima/netty_05_future/TestNettyFutureListener.java
0 → 100644
浏览文件 @
7971cb27
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
src/main/java/com/kwan/shuyu/heima/netty_05_future/TestNettyPromiseFailure.java
0 → 100644
浏览文件 @
7971cb27
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
src/main/java/com/kwan/shuyu/heima/netty_05_future/TestNettyPromiseSuccess.java
0 → 100644
浏览文件 @
7971cb27
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录