feat(新增): 🚀 NettyFuture获取结果
This commit is contained in:
parent
41a4c7f388
commit
995953b40c
|
@ -2,7 +2,7 @@
|
|||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.nio.channels.ServerSocketChannel,open" />
|
||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.nio.channels.ServerSocketChannel,open,java.util.concurrent.Executors,newFixedThreadPool" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
|
@ -0,0 +1,21 @@
|
|||
package cn.bunny.demo5;
|
||||
|
||||
import io.netty.channel.EventLoop;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.util.concurrent.Future;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class TestAsyncFuture {
|
||||
public static void main(String[] args) {
|
||||
NioEventLoopGroup loopGroup = new NioEventLoopGroup();
|
||||
EventLoop eventLoop = loopGroup.next();
|
||||
Future<Integer> future = eventLoop.submit(() -> {
|
||||
log.debug("执行计算...");
|
||||
Thread.sleep(1000);
|
||||
return 70;
|
||||
});
|
||||
|
||||
future.addListener(future1 -> log.debug("接受结果:{}", future1.getNow()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package cn.bunny.demo5;
|
||||
|
||||
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.ExecutionException;
|
||||
|
||||
@Slf4j
|
||||
public class TestNettyFuture {
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
NioEventLoopGroup group = new NioEventLoopGroup();
|
||||
EventLoop eventLoop = group.next();
|
||||
|
||||
Future<Integer> future = eventLoop.submit(() -> {
|
||||
Thread.sleep(1000);
|
||||
return 70;
|
||||
});
|
||||
|
||||
// 主线程通过 future 来获取结果
|
||||
log.debug("主线程等待结果");
|
||||
future.get();
|
||||
log.debug("结果是:{}", future.get());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package cn.bunny.demo5;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@Slf4j
|
||||
public class UdkFuture {
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
// 线程池
|
||||
ExecutorService service = Executors.newFixedThreadPool(2);
|
||||
// 提交任务
|
||||
Future<Integer> future = service.submit(() -> {
|
||||
Thread.sleep(1000);
|
||||
return 50;
|
||||
});
|
||||
|
||||
// 主线程通过 future 来获取结果
|
||||
log.debug("主线程等待结果");
|
||||
future.get();
|
||||
log.debug("结果是:{}", future.get());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue