`
genier200
  • 浏览: 22802 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类

Executors.newSingleThreadScheduledExecutor【持续更新】

 
阅读更多
工作中遇到的问题:
【问题现象】:
使用
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleWithFixedDelay(new Runnable() {
             public void run() {
                 //...CODE             }
        }, 1, 10, TimeUnit.SECONDS);
多数的task使用此方法能够正常执行,本人编写的代码中使用此方法,却不能正常执行,系统自动将此连接池对象thread置为无限等待状态:WAITING,无法周期执行,导致问题产生。

【解决】
1,变量保存提交Task后的Future。

2,针对future变量,使用get,如果此线程提交后在运行时发生了,运行时异常,将可以抛出,便于抓取结果分析。

3,在线程中编写try-catch等,不一定有作用,不能捕捉到运行时异常。
分享到:
评论
1 楼 gandilong 2013-01-28  
:wink:

相关推荐

Global site tag (gtag.js) - Google Analytics