首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Java Timer的容易应用

2012-10-24 
Java Timer的简单应用最近由于工作关系,要使用Java中的Timer,从网上的评论得知这个东东并不是很好用,由于

Java Timer的简单应用

最近由于工作关系,要使用Java中的Timer,从网上的评论得知这个东东并不是很好用,由于需求也不是太苛刻,所以把它改改就OK了。需求是这样的:

    .在请求到来时开始计时。 在请求超过规定时间时,结束计时。 在得到页面确认时,重置时间,重新开始计时。

需求很简单,下面是我的定时器实现:

  1. package?com.youcompany.yourproject.comet; ??
  2. ??
  3. import?java.util.Date; ??
  4. import?java.util.Timer; ??
  5. import?java.util.TimerTask; ??
  6. ??
  7. import?com.youcompany.yourproject.core.common.Constants; ??
  8. import?com.youcompany.yourproject.core.common.DFPropertyOwner; ??
  9. import?com.youcompany.yourproject.core.error.ParameterException; ??
  10. import?com.youcompany.yourproject.service.JkControlService; ??
  11. ??
  12. public?class?CometTimer ??
  13. { ??
  14. ????private?final?Timer?timer?=?new?Timer();? ??
  15. ????private?TaskTime?myTime?=?new?TaskTime(0,DFPropertyOwner.getKeyNumber("userstate")); ??
  16. ????public?CometTimer() ??
  17. ????{ ??
  18. ????} ??
  19. ????public?void?start(ServerThread?thread)? ??
  20. ????{? ??
  21. ????????myTime.setService(thread); ??
  22. ????????timer.schedule(myTime,new?Date(),Constants.STOPFORQUERY?*?1000); ??
  23. ????}??? ??
  24. ????public?void?restart() ??
  25. ????{ ??
  26. ????????myTime.restart(); ??
  27. ????} ??
  28. ????private?class?TaskTime?extends?TimerTask ??
  29. ????{ ??
  30. ????????private?ServerThread?service; ??
  31. ????????private?int?counter?=?0;//当前技术值 ??
  32. ????????private?int?all?=?0;//计数最大值 ??
  33. ????????private?boolean?isResc?=?false;//升序还是降,默认升序。 ??
  34. ????????@Override??
  35. ????????public?void?run() ??
  36. ????????{ ??
  37. ????????????//?TODO?Auto-generated?method?stub ??
  38. //??????????System.out.println("fdsafdsafdsafdsafdsafdsaf"); ??
  39. ????????????if(this.isResc) ??
  40. ????????????{ ??
  41. ????????????????if((counter--)==?0) ??
  42. ????????????????{ ??
  43. ????????????????????doWork(); ??
  44. ????????????????} ??
  45. ????????????} ??
  46. ????????????else??
  47. ????????????{ ??
  48. ????????????????if((this.counter++)==?(this.all?-?1)) ??
  49. ????????????????{ ??
  50. ????????????????????doWork(); ??
  51. ????????????????} ??
  52. ????????????} ??
  53. ????????} ??
  54. ????????public?void?doWork() ??
  55. ????????{ ??
  56. ????????????System.out.println("User?is?out?of?time!!"); ??
  57. ????????????if(this.service?==?null) ??
  58. ????????????{ ??
  59. ????????????????throw?new?ParameterException("name"); ??
  60. ????????????} ??
  61. ????????????else??
  62. ????????????{ ??
  63. ????????????????JkControlService.leave(service); ??
  64. ????????????} ??
  65. ????????????timer.cancel(); ??
  66. ????????} ??
  67. ????????public??TaskTime(int?i?,?int?j) ??
  68. ????????{ ??
  69. ????????????//?TODO?Auto-generated?method?stub ??
  70. ????????????this.setAll(j); ??
  71. ????????????this.setCounter(i); ??
  72. ????????}??? ??
  73. ????????/** ?
  74. ?????????*?设置初始值 ?
  75. ?????????*?@param?i:?初始值 ?
  76. ?????????*?@param?j:?技数个数 ?
  77. ?????????*/??
  78. ????????public??TaskTime(int?i?,?int?j,boolean?isRESC) ??
  79. ????????{ ??
  80. ????????????//?TODO?Auto-generated?method?stub ??
  81. ????????????this.setAll(j); ??
  82. ????????????this.setCounter(i); ??
  83. ????????????this.setResc(isRESC); ??
  84. ????????} ??
  85. ????????public?void?setCounter(int?counter) ??
  86. ????????{ ??
  87. ????????????if(counter?>?0) ??
  88. ????????????????this.counter?=?counter; ??
  89. ????????} ??
  90. ????????public?void?setAll(int?all) ??
  91. ????????{ ??
  92. ????????????if(all?>this.counter) ??
  93. ????????????????this.all?=?all; ??
  94. ????????} ??
  95. ????????public?boolean?isResc() ??
  96. ????????{ ??
  97. ????????????return?isResc; ??
  98. ????????} ??
  99. ????????public?void?restart() ??
  100. ????????{ ??
  101. ????????????if(this.isResc) ??
  102. ????????????{ ??
  103. ????????????????this.counter?=?this.all; ??
  104. ????????????} ??
  105. ????????????else??
  106. ????????????{ ??
  107. ????????????????this.counter?=?0; ??
  108. ????????????} ??
  109. ????????} ??
  110. ????????/** ?
  111. ?????????*?是否是递减记数。 ?
  112. ?????????*?@param?isResc ?
  113. ?????????*/??
  114. ????????public?void?setResc(boolean?isResc) ??
  115. ????????{ ??
  116. ????????????????this.isResc?=?isResc; ??
  117. ????????} ??
  118. ????????public?void?setService(ServerThread?service) ??
  119. ????????{ ??
  120. ????????????this.service?=?service; ??
  121. ????????} ??
  122. ????} ??
  123. } ??
此定时器主要是通过一个计数器来实现定时功能,使用计数器主要是因为Java提供的Timer中好象没有重新计时的功能。所以只能使用这种自欺欺人的方法了。如果大家有什么更好发使用Timer的方法,欢迎贴出来大家探讨一下。 1 楼 Jatula 2008-09-16   以前用过一次,但是感觉,做些没用的工作,他真的不错,像打印一句话之类的,很灵,但在里面再个方法处理,马上挂掉;也挺郁闷的;

热点排行