{"id":7558,"date":"2011-08-04T08:00:14","date_gmt":"2011-08-03T23:00:14","guid":{"rendered":"http:\/\/www.sssg.org\/blogs\/hiro345\/?p=7558"},"modified":"2011-08-03T09:56:51","modified_gmt":"2011-08-03T00:56:51","slug":"java-concurrent%e3%83%91%e3%83%83%e3%82%b1%e3%83%bc%e3%82%b8%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9ftimer","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/7558.html","title":{"rendered":"Java Concurrent\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3063\u305fTimer"},"content":{"rendered":"<p>\u30bf\u30a4\u30de\u30fc\u30cd\u30bf\u304c\u7d9a\u3044\u3066\u3044\u307e\u3059\u304c\u3001Java Concurrent\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3063\u305fTimer\u3082\u3067\u304d\u308b\u3002\u3069\u306e\u30bf\u30a4\u30de\u30fc\u3082\u9577\u6240\u3068\u77ed\u6240\u304c\u3042\u308b\u306e\u3067\u3001\u7528\u9014\u306b\u5fdc\u3058\u3066\u3069\u308c\u3092\u5229\u7528\u3059\u308c\u3070\u826f\u3044\u304b\u6c7a\u3081\u308c\u3070\u3088\u3044\u3060\u308d\u3046\u3002<\/p>\n<p><!--more--><br \/>\nJava Concurrent\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3063\u305fTimer\u306f\u3001java.util.concurrent.ScheduledExecutorService\u3092\u4f7f\u3048\u3070\u5b9f\u73fe\u3067\u304d\u308b\u3002Timer\u306e\u5909\u308f\u308a\u306bScheduledExecutorService\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306escheduler\u3092\u4f7f\u3063\u3066\u3001\u505c\u6b62\u3055\u305b\u308b\u3068\u304d\u306b\u306f\u3053\u306e\u8fd4\u5374\u5024\u306eScheduledFuture\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306ecancel\u3092\u547c\u3093\u3067\u3044\u308b\u3002stop\u30e1\u30bd\u30c3\u30c9\u3092task\u306erun\u30e1\u30bd\u30c3\u30c9\u304b\u3089\u547c\u3073\u51fa\u3057\u3066\u3044\u308b\u3042\u305f\u308a\u3067\u53c2\u7167\u95a2\u4fc2\u304c\u6c17\u306b\u306a\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u304c\u3001\u3068\u308a\u3042\u3048\u305a\u30b5\u30f3\u30d7\u30eb\u306a\u306e\u3067\u3053\u3046\u3057\u3066\u3044\u308b\u3002scheduler\u306e\u7d42\u4e86\u51e6\u7406\u306b\u3064\u3044\u3066\u3082\u8a18\u8f09\u3057\u3066\u304a\u304d\u305f\u304b\u3063\u305f\u306e\u3067\u3001\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u306712\u79d2\u5f85\u6a5f\u3057\u3066\u304b\u3089shutdownNow\u3059\u308b\u30b3\u30fc\u30c9\u306b\u3057\u3066\u3042\u308b\u3002<\/p>\n<pre class=\"brush: java; gutter: true\">\r\npackage&nbsp;org.sssg.soft.sample.timer&#x3b;\r\n\r\nimport&nbsp;java.util.concurrent.Executors&#x3b;\r\nimport&nbsp;java.util.concurrent.ScheduledExecutorService&#x3b;\r\nimport&nbsp;java.util.concurrent.ScheduledFuture&#x3b;\r\nimport&nbsp;java.util.concurrent.TimeUnit&#x3b;\r\n\r\npublic&nbsp;class&nbsp;SimpleConcurrentTimer&nbsp;{\r\n&nbsp;&nbsp;private&nbsp;final&nbsp;static&nbsp;long&nbsp;PERIOD&nbsp;=&nbsp;1000L&#x3b;\r\n&nbsp;&nbsp;private&nbsp;Runnable&nbsp;task&#x3b;\r\n&nbsp;&nbsp;private&nbsp;ScheduledExecutorService&nbsp;scheduler&#x3b;\r\n&nbsp;&nbsp;private&nbsp;ScheduledFuture&lt;?&gt;&nbsp;future&#x3b;\r\n&nbsp;&nbsp;private&nbsp;final&nbsp;static&nbsp;int&nbsp;COUNT&nbsp;=&nbsp;10&#x3b;\r\n\r\n&nbsp;&nbsp;public&nbsp;SimpleConcurrentTimer()&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;task&nbsp;=&nbsp;new&nbsp;Runnable()&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i&nbsp;=&nbsp;COUNT&#x3b;\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;run()&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(i&#x2d;&#x2d;)&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(i&nbsp;&lt;&nbsp;0)&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stop()&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;}&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;scheduler&nbsp;=&nbsp;Executors.newSingleThreadScheduledExecutor()&#x3b;\r\n&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;public&nbsp;void&nbsp;stop()&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;scheduler.schedule(new&nbsp;Runnable()&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;run()&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;future.cancel(true)&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;future&nbsp;canceled&quot;)&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;},&nbsp;0,&nbsp;TimeUnit.SECONDS)&#x3b;\r\n&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;public&nbsp;void&nbsp;run()&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;future&nbsp;start&quot;)&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;future&nbsp;=&nbsp;scheduler.scheduleAtFixedRate(task,&nbsp;0,&nbsp;PERIOD,\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeUnit.MILLISECONDS)&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(12&nbsp;*&nbsp;1000)&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(InterruptedException&nbsp;e)&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace()&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;scheduler.shutdownNow()&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;shutdowned&quot;)&#x3b;\r\n&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;public&nbsp;static&nbsp;final&nbsp;void&nbsp;main(final&nbsp;String[]&nbsp;args)&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;SimpleConcurrentTimer&nbsp;app&nbsp;=&nbsp;new&nbsp;SimpleConcurrentTimer()&#x3b;\r\n&nbsp;&nbsp;&nbsp;&nbsp;app.run()&#x3b;\r\n&nbsp;&nbsp;}\r\n}\r\n<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001Executor\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001Executors\u306estatic\u306a\u30d5\u30a1\u30af\u30c8\u30ea\u30fb\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u751f\u6210\u3067\u304d\u308b\u3002ScheduledExecutor\u306b\u306fnewScheduledThreadPool\u3084newSingleThreadScheduledExecutor\u304c\u3042\u308b\u3002\u524d\u8005\u306f\u30b9\u30ec\u30c3\u30c9\u30d7\u30fc\u30eb\u3092\u4f5c\u308b\u3053\u3068\u304b\u3089\u8907\u6570\u30b9\u30ec\u30c3\u30c9\u5bfe\u5fdc\u7248\u3067\u3001\u5f8c\u8005\u306f\u5358\u4e00\u30b9\u30ec\u30c3\u30c9\u5bfe\u5fdc\u7248\u306eScheduledExecutor\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u751f\u6210\u3068\u306a\u308b\u3002\u3053\u3053\u3067\u306f\u3001newSingleThreadScheduledExecutor\u3092\u4f7f\u3063\u3066\u3044\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30bf\u30a4\u30de\u30fc\u30cd\u30bf\u304c\u7d9a\u3044\u3066\u3044\u307e\u3059\u304c\u3001Java Concurrent\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3063\u305fTimer\u3082\u3067\u304d\u308b\u3002\u3069\u306e\u30bf\u30a4\u30de\u30fc\u3082\u9577\u6240\u3068\u77ed\u6240\u304c\u3042\u308b\u306e\u3067\u3001\u7528\u9014\u306b\u5fdc\u3058\u3066\u3069\u308c\u3092\u5229\u7528\u3059\u308c\u3070\u826f\u3044\u304b\u6c7a\u3081\u308c\u3070\u3088\u3044\u3060\u308d\u3046\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[550,1207,547],"class_list":["post-7558","post","type-post","status-publish","format-standard","hentry","category-java","tag-concurrent","tag-java","tag-timer"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/7558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=7558"}],"version-history":[{"count":1,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/7558\/revisions"}],"predecessor-version":[{"id":7559,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/7558\/revisions\/7559"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=7558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=7558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=7558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}