java.util.concurrent.scheduledthreadpoolexecutor 是jdk1 .6之后自带的包,功能强大,能实现定时器和延时加载的功能
各类功能和处理方面优于 timer
1、定时器:
scheduledthreadpoolexecutor 有个scheduleatfixedrate(command, initialdelay, period, unit) ;方法 command: 执行的线程(可自己new一个) initialdelay:初始化执行的延时时间 period: 时间间隔 unit : 时间类型(如timeunit.seconds: 秒的方式执行,timeunit.days : 天数的方式执行)具体代码:
1 2 3 4 5 6 7 8 9 10 |
public static void main(string[] args) { stthread.scheduleatfixedrate( new runnable() { @override public void run() { // todo auto-generated method stub system.out.println( new date()); } }, 20 , 2 , timeunit.seconds); } |
2、延时处理
scheduledthreadpoolexecutor 有个 schedule(callable, delay, unit) ; 方法 callable:回调方法 delay:延时时间 unit:时间类型,同定时器的unit一样具体代码:
1 |
stthread.schedule((roleprvlegetask)springutils.getbean( "roleprvlegetask" ), 1 , timeunit.seconds); |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/moneyshi/article/details/45314059
查看更多关于Java自带定时任务ScheduledThreadPoolExecutor实现定时器和延时加载功能的详细内容...