http://www.tianjiaguo.com/programming-language/java-language/netty%E5%AE%9A%E6%97%B6%E5%99%A8hashedwheeltimer/

netty中的Timer管理,使用了的Hashed time Wheel的模式,Time Wheel翻译为时间轮,是用于实现定时器timer的经典算法。

我们看看netty的HashedWheelTimer的一个测试的例子,先new一个HashedWheelTimer,然后调用它的newTimeout方法,这个方法的声明是这样的:


    /**
     * Schedules the specified {@link TimerTask} for one-time execution after
     * the specified delay.
     *
     * @return a handle which is associated with the specified task
     *
     * @throws IllegalStateException if this timer has been
     *                               {@linkplain #stop() stopped} already
     */
    Timeout newTimeout(TimerTask task, long delay, TimeUnit unit);

这个方法需要一个TimerTask对象以知道当时间到时要执行什么逻辑,然后需要delay时间数值和TimeUnit时间的单位,像下面的例子中,我们在timer到期后会打印字符串,第一个任务是5秒后开始执行,第二个10秒后开始执行。


import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.Timer;
import org.jboss.netty.util.TimerTask;


import java.util.concurrent.TimeUnit;

/**
 * 12-6-6 下午2:46
 *
 * @author jiaguotian Copyright 2012 Sohu.com Inc. All Rights Reserved.
 */
public class TimeOutTest {
    public static void main(String[] argv) {
        final Timer timer = new HashedWheelTimer();
        timer.newTimeout(new TimerTask() {
            public void run(Timeout timeout) throws Exception {
                System.out.println("timeout 5");
            }
        }, 5, TimeUnit.SECONDS);
        timer.newTimeout(new TimerTask() {
            public void run(Timeout timeout) throws Exception {
                System.out.println("timeout 10");
            }
        }, 10, TimeUnit.SECONDS);
    }
}

参考资料:
1:http://blog.hummingbird-one.com/?p=10048
2:

netty定时器HashedWheelTimer(zz)的更多相关文章

  1. netty研究【1】:编译源代码

    netty作为异步通信底层框架,其优异的性能让我产生了研究他的源码的决定. 代码研究之前,第一步就是要准备环境,至少可以编译通过,下面,就拿github上的4.1分支进行.我的IDE是Intellij ...

  2. HashedWheelTimer 原理

    HashedWheelTimer 是根据 Hashed and Hierarchical Timing Wheels: Data Structuresfor the Efficient Impleme ...

  3. HashedWheelTimer

    HashedWheelTimer 是根据 Hashed and Hierarchical Timing Wheels: Data Structuresfor the Efficient Impleme ...

  4. Netty 之 Netty生产级的心跳和重连机制

    https://blog.csdn.net/z69183787/article/details/52625095 最近工作比较忙,但闲暇之余还是看了阿里的冯家春(fengjiachun)的github ...

  5. Netty生产级的心跳和重连机制

    今天研究的是,心跳和重连,虽然这次是大神写的代码,但是万变不离其宗,我们先回顾一下Netty应用心跳和重连的整个过程: 1)客户端连接服务端 2)在客户端的的ChannelPipeline中加入一个比 ...

  6. netty之管道处理流程

    1.我们在使用netty的是有都会存在将channelBuffer的数据处理成相应的String或者自定义数据.而这里主要是介绍管道里面存在的上行和下行的数据处理方式 2.通过一张图片来看一下具体管道 ...

  7. netty之心跳机制

    1.心跳机制,在netty3和netty5上面都有.但是写法有些不一样. 2.心跳机制在服务端和客户端的作用也是不一样的.对于服务端来说:就是定时清除那些因为某种原因在一定时间段内没有做指定操作的客户 ...

  8. NETTY 心跳机制

    最近工作比较忙,但闲暇之余还是看了阿里的冯家春(fengjiachun)的github上的开源代码Jupiter,写的RPC框架让我感叹人外有人,废话不多说,下面的代码全部截取自Jupiter,写了一 ...

  9. netty 实现心跳检查--断开重连--通俗易懂

    一.心跳介绍 网络中的接收和发送数据都是使用操作系统中的SOCKET进行实现.但是如果此套接字已经断开,那发送数据和接收数据的时候就一定会有问题. 1.心跳机制: 是服务端和客户端定时的发送一个心跳包 ...

随机推荐

  1. 在XcodeGhost事件之后,获取更纯净的Xcode的方法。

    正值Xcode 7正式版本的更新,IOS界就冒出了个甚至可以说成涉及国家安全的大事也不为过的事件: 也可以点击网址链接看总结的更完整的文章:众多知名 APP 都中毒了,XCodeGhost 病毒事件汇 ...

  2. SQL Server 查看存储过程执行次数的方法

    今天老大提出一个需求,想查看数据库存储过程执行的次数,以前没有接触过,于是网上找了下,发现还真有! 不废话,贴出来sql语句,直接执行即可看到结果: use master select text,ex ...

  3. 关于手机微网站ICP备案

    今天终于拨通了陕西省通信管理局的电话,并告诉对方我们做的是一个化妆品的微网站,会涉及到使用使用支付宝支付. 询问"xxx微网站"网站经营类型,对方告知虽然使用支付宝,但是是微网站, ...

  4. Linux学习书目

    Linux基础 1.<Linux与Unix Shell 编程指南> C语言基础 1.<C Primer Plus,5th Edition>[美]Stephen Prata著 2 ...

  5. javascripts 实习自动提交表单 onsubmit

    html: <form id="formwb" onsubmit="return setPassword();"> <script> d ...

  6. Effective Java 43 Return empty arrays or collections, not nulls

    Feature Return empty arrays or collection Return nulls Avoids the expense of allocating the array N ...

  7. SQL基础(1)-创建及修改表

    1. 建表语句 CREATE TABLE fdh_client_info ( id varchar2(50) primary key, name varchar2(30) not null, sex ...

  8. cocos2d-x之悦动的小球

    发现问题:update()函数不能用virtual前缀 主: bool HelloWorld::init() { if ( !LayerColor::initWithColor(Color4B(255 ...

  9. Linux查看BIOS信息

    http://www.linuxde.net/2013/02/12499.html

  10. UESTC 915 方老师的分身II --最短路变形

    即求从起点到终点至少走K条路的最短路径. 用两个变量来维护一个点的dis,u和e,u为当前点的编号,e为已经走过多少条边,w[u][e]表示到当前点,走过e条边的最短路径长度,因为是至少K条边,所以大 ...