netty NioEventLoop 实例
所属分类 netty
浏览量 90
io.netty.channel.EventLoop
EventLoop 接口继承链
EventLoop > EventLoopGroup > EventExecutorGroup > ScheduledExecutorService(JDK) > ExecutorService > Executor
io.netty.util.concurrent.EventExecutorGroup
public interface EventExecutorGroup extends ScheduledExecutorService, Iterable< EventExecutor> {
EventExecutor next();
Future< ?> submit(Runnable task);
@Override
< T> Future< T> submit(Runnable task, T result);
@Override
< T> Future< T> submit(Callable< T> task);
@Override
ScheduledFuture< ?> schedule(Runnable command, long delay, TimeUnit unit);
@Override
< V> ScheduledFuture< V> schedule(Callable< V> callable, long delay, TimeUnit unit);
@Override
ScheduledFuture< ?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
@Override
ScheduledFuture< ?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
public class NioEventLoopDemo {
public static void main(String[] args) throws Exception{
NioEventLoopGroup parent = new NioEventLoopGroup(3);
System.out.println(parent.toString());
EventLoop eventLoop = parent.next();
System.out.println(eventLoop.getClass()+"");
System.out.println(eventLoop+"");
parent.execute(new Task());
parent.schedule(new DelayTask(),1, TimeUnit.SECONDS);
parent.scheduleAtFixedRate(new PeriodTask("parent_001"),0,2, TimeUnit.SECONDS);
parent.scheduleAtFixedRate(new PeriodTask("parent_002"),0,2, TimeUnit.SECONDS);
Thread.sleep(3000);
eventLoop.execute(new Task());
eventLoop.schedule(new DelayTask(),1, TimeUnit.SECONDS);
eventLoop.scheduleAtFixedRate(new PeriodTask("child_001"),0,2, TimeUnit.SECONDS);
Thread.sleep(3000000);
}
private static class Task implements Runnable{
@Override
public void run(){
System.out.println("task run,"+Thread.currentThread()+","+ LocalDateTime.now());
}
}
private static class DelayTask implements Runnable{
@Override
public void run(){
System.out.println("DelayTask run,"+Thread.currentThread()+ LocalDateTime.now());
}
}
private static class PeriodTask implements Runnable{
final String name;
public PeriodTask(String name){
this.name=name;
}
@Override
public void run(){
System.out.println("PeriodTask ["+name+"] run,"+Thread.currentThread()+ LocalDateTime.now());
}
}
}
io.netty.util.concurrent.AbstractEventExecutorGroup
public void execute(Runnable command) {
// 绑定一个 eventloop 执行
next().execute(command);
}
public ScheduledFuture< ?> schedule(Runnable command, long delay, TimeUnit unit) {
return next().schedule(command, delay, unit);
}
https://gitee.com/dyyx/netty-learning-example/blob/master/netty-iot/src/main/java/com/sanshengshui/iot/test/NioEventLoopDemo.java
上一篇
下一篇
grep 日志搜索技巧
flink jar包上传目录设置
netty NioEventLoop 性能优化与最佳实践
grafana API 调用 Bearer Token
道友入门
道家养生功(牛胜先)