{"action":"created","comment":{"url":"https://api.github.com/repos/netty/netty/pulls/comments/24675209","id":24675209,"diff_hunk":"@@ -0,0 +1,236 @@\n+/*\n+ * Copyright 2015 The Netty Project\n+ *\n+ * The Netty Project licenses this file to you under the Apache License,\n+ * version 2.0 (the \"License\"); you may not use this file except in compliance\n+ * with the License. You may obtain a copy of the License at:\n+ *\n+ *   http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+ * License for the specific language governing permissions and limitations\n+ * under the License.\n+ */\n+package io.netty.util.concurrent;\n+\n+import java.util.Iterator;\n+import java.util.PriorityQueue;\n+import java.util.Queue;\n+import java.util.concurrent.Callable;\n+import java.util.concurrent.Executors;\n+import java.util.concurrent.TimeUnit;\n+\n+/**\n+ * Abstract base class for {@link EventExecutor}s that want to support scheduling.\n+ */\n+public abstract class AbstractScheduledEventExecutor extends AbstractEventExecutor {\n+\n+    Queue<ScheduledFutureTask<?>> scheduledTaskQueue;\n+\n+    protected static long nanoTime() {\n+        return ScheduledFutureTask.nanoTime();\n+    }\n+\n+    Queue<ScheduledFutureTask<?>> scheduledTaskQueue() {\n+        if (scheduledTaskQueue == null) {\n+            scheduledTaskQueue = new PriorityQueue<ScheduledFutureTask<?>>();\n+        }\n+        return scheduledTaskQueue;\n+    }\n+\n+    /**\n+     * Cancel all scheduled tasks.\n+     *\n+     * This method MUST be called only when {@link #inEventLoop()} is {@code true}.\n+     */\n+    protected final void cancelDelayedTasks() {\n+        assert inEventLoop();\n+        Queue<ScheduledFutureTask<?>> delayedTaskQueue = scheduledTaskQueue;\n+        if (delayedTaskQueue == null || delayedTaskQueue.isEmpty()) {\n+            return;\n+        }\n+\n+        final ScheduledFutureTask<?>[] delayedTasks =\n+                delayedTaskQueue.toArray(new ScheduledFutureTask<?>[delayedTaskQueue.size()]);\n+\n+        for (ScheduledFutureTask<?> task: delayedTasks) {\n+            task.cancel(false);\n+        }\n+\n+        delayedTaskQueue.clear();\n+    }\n+\n+    /**\n+     * @see {@link #pollScheduledTask(long)}\n+     */\n+    protected final Runnable pollScheduledTask() {\n+        return pollScheduledTask(nanoTime());\n+    }\n+\n+    /**\n+     * Return the {@link Runnable} which is ready to be executed with the given {@code nanoTime}.\n+     * You should use {@link #nanoTime()} to retrieve the the correct {@code nanoTime}.\n+     */\n+    protected final Runnable pollScheduledTask(long nanoTime) {\n+        assert inEventLoop();\n+\n+        Queue<ScheduledFutureTask<?>> delayedTaskQueue = scheduledTaskQueue;\n+        ScheduledFutureTask<?> delayedTask = delayedTaskQueue == null ? null : delayedTaskQueue.peek();\n+        if (delayedTask == null) {\n+            return null;\n+        }\n+\n+        if (delayedTask.deadlineNanos() <= nanoTime) {\n+            delayedTaskQueue.remove();\n+            return delayedTask;\n+        }\n+        return null;\n+    }\n+\n+    /**\n+     * Return the nanoseconds when the next scheduled task is ready to be run or {@code -1} if no task is scheduled.\n+     */\n+    protected final long nextScheduledTaskNano() {\n+        assert checkInEventLoop();\n+\n+        Queue<ScheduledFutureTask<?>> delayedTaskQueue = scheduledTaskQueue;\n+        ScheduledFutureTask<?> delayedTask = delayedTaskQueue == null ? null : delayedTaskQueue.peek();","path":"common/src/main/java/io/netty/util/concurrent/AbstractScheduledEventExecutor.java","position":99,"original_position":99,"commit_id":"2fd660276ecd80aa7ca63e1cd58cce440f11b0ca","original_commit_id":"2fd660276ecd80aa7ca63e1cd58cce440f11b0ca","user":{"login":"Scottmitch","id":7562868,"avatar_url":"https://avatars.githubusercontent.com/u/7562868?v=3","gravatar_id":"","url":"https://api.github.com/users/Scottmitch","html_url":"https://github.com/Scottmitch","followers_url":"https://api.github.com/users/Scottmitch/followers","following_url":"https://api.github.com/users/Scottmitch/following{/other_user}","gists_url":"https://api.github.com/users/Scottmitch/gists{/gist_id}","starred_url":"https://api.github.com/users/Scottmitch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Scottmitch/subscriptions","organizations_url":"https://api.github.com/users/Scottmitch/orgs","repos_url":"https://api.github.com/users/Scottmitch/repos","events_url":"https://api.github.com/users/Scottmitch/events{/privacy}","received_events_url":"https://api.github.com/users/Scottmitch/received_events","type":"User","site_admin":false},"body":"Consider re-arranging.","created_at":"2015-02-13T16:18:58Z","updated_at":"2015-02-13T16:18:58Z","html_url":"https://github.com/netty/netty/pull/3381#discussion_r24675209","pull_request_url":"https://api.github.com/repos/netty/netty/pulls/3381","_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/24675209"},"html":{"href":"https://github.com/netty/netty/pull/3381#discussion_r24675209"},"pull_request":{"href":"https://api.github.com/repos/netty/netty/pulls/3381"}}},"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/3381","id":28382156,"html_url":"https://github.com/netty/netty/pull/3381","diff_url":"https://github.com/netty/netty/pull/3381.diff","patch_url":"https://github.com/netty/netty/pull/3381.patch","issue_url":"https://api.github.com/repos/netty/netty/issues/3381","number":3381,"state":"open","locked":false,"title":"Allow to use EmbeddedChannel.schedule*(...)","user":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"body":"Motivation:\r\n\r\nAt the moment when EmbeddedChannel is used and a ChannelHandler tries to schedule and task it will throw an UnsupportedOperationException. This makes it impossible to test these handlers or even reuse them with EmbeddedChannel.\r\n\r\nModifications:\r\n\r\n- Factor out reusable scheduling code into AbstractSchedulingEventExecutor\r\n- Let EmbeddedEventLoop and SingleThreadEventExecutor extend AbstractSchedulingEventExecutor\r\n- add EmbbededChannel.runScheduledPendingTasks() which allows to run all scheduled tasks that are ready\r\n\r\nResult:\r\n\r\nEmbeddedchannel is now usable even with ChannelHandler that try to schedule tasks.","created_at":"2015-01-30T18:29:44Z","updated_at":"2015-02-13T16:18:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5e0e54147b0b3d1e1db2f7a6928318004d0a544e","assignee":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/netty/netty/milestones/109","labels_url":"https://api.github.com/repos/netty/netty/milestones/109/labels","id":918308,"number":109,"title":"4.0.26.Final","description":null,"creator":{"login":"trustin","id":173918,"avatar_url":"https://avatars.githubusercontent.com/u/173918?v=3","gravatar_id":"","url":"https://api.github.com/users/trustin","html_url":"https://github.com/trustin","followers_url":"https://api.github.com/users/trustin/followers","following_url":"https://api.github.com/users/trustin/following{/other_user}","gists_url":"https://api.github.com/users/trustin/gists{/gist_id}","starred_url":"https://api.github.com/users/trustin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trustin/subscriptions","organizations_url":"https://api.github.com/users/trustin/orgs","repos_url":"https://api.github.com/users/trustin/repos","events_url":"https://api.github.com/users/trustin/events{/privacy}","received_events_url":"https://api.github.com/users/trustin/received_events","type":"User","site_admin":false},"open_issues":54,"closed_issues":45,"state":"open","created_at":"2014-12-31T07:43:52Z","updated_at":"2015-02-13T05:16:41Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/netty/netty/pulls/3381/commits","review_comments_url":"https://api.github.com/repos/netty/netty/pulls/3381/comments","review_comment_url":"https://api.github.com/repos/netty/netty/pulls/comments/{number}","comments_url":"https://api.github.com/repos/netty/netty/issues/3381/comments","statuses_url":"https://api.github.com/repos/netty/netty/statuses/2fd660276ecd80aa7ca63e1cd58cce440f11b0ca","head":{"label":"netty:embedded_schedule","ref":"embedded_schedule","sha":"2fd660276ecd80aa7ca63e1cd58cce440f11b0ca","user":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"repo":{"id":1064563,"name":"netty","full_name":"netty/netty","owner":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/netty/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":false,"url":"https://api.github.com/repos/netty/netty","forks_url":"https://api.github.com/repos/netty/netty/forks","keys_url":"https://api.github.com/repos/netty/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/netty/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/netty/netty/teams","hooks_url":"https://api.github.com/repos/netty/netty/hooks","issue_events_url":"https://api.github.com/repos/netty/netty/issues/events{/number}","events_url":"https://api.github.com/repos/netty/netty/events","assignees_url":"https://api.github.com/repos/netty/netty/assignees{/user}","branches_url":"https://api.github.com/repos/netty/netty/branches{/branch}","tags_url":"https://api.github.com/repos/netty/netty/tags","blobs_url":"https://api.github.com/repos/netty/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/netty/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/netty/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/netty/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/netty/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/netty/netty/languages","stargazers_url":"https://api.github.com/repos/netty/netty/stargazers","contributors_url":"https://api.github.com/repos/netty/netty/contributors","subscribers_url":"https://api.github.com/repos/netty/netty/subscribers","subscription_url":"https://api.github.com/repos/netty/netty/subscription","commits_url":"https://api.github.com/repos/netty/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/netty/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/netty/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/netty/netty/issues/comments/{number}","contents_url":"https://api.github.com/repos/netty/netty/contents/{+path}","compare_url":"https://api.github.com/repos/netty/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/netty/netty/merges","archive_url":"https://api.github.com/repos/netty/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/netty/netty/downloads","issues_url":"https://api.github.com/repos/netty/netty/issues{/number}","pulls_url":"https://api.github.com/repos/netty/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/netty/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/netty/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/netty/netty/labels{/name}","releases_url":"https://api.github.com/repos/netty/netty/releases{/id}","created_at":"2010-11-09T09:22:21Z","updated_at":"2015-02-13T11:15:55Z","pushed_at":"2015-02-13T08:35:19Z","git_url":"git://github.com/netty/netty.git","ssh_url":"git@github.com:netty/netty.git","clone_url":"https://github.com/netty/netty.git","svn_url":"https://github.com/netty/netty","homepage":"http://netty.io/","size":107783,"stargazers_count":4159,"watchers_count":4159,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2019,"mirror_url":null,"open_issues_count":228,"forks":2019,"open_issues":228,"watchers":4159,"default_branch":"master"}},"base":{"label":"netty:4.0","ref":"4.0","sha":"5f71b6dc54d7cb806f3acccf632ef6c3cbe9d1c9","user":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"repo":{"id":1064563,"name":"netty","full_name":"netty/netty","owner":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/netty/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":false,"url":"https://api.github.com/repos/netty/netty","forks_url":"https://api.github.com/repos/netty/netty/forks","keys_url":"https://api.github.com/repos/netty/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/netty/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/netty/netty/teams","hooks_url":"https://api.github.com/repos/netty/netty/hooks","issue_events_url":"https://api.github.com/repos/netty/netty/issues/events{/number}","events_url":"https://api.github.com/repos/netty/netty/events","assignees_url":"https://api.github.com/repos/netty/netty/assignees{/user}","branches_url":"https://api.github.com/repos/netty/netty/branches{/branch}","tags_url":"https://api.github.com/repos/netty/netty/tags","blobs_url":"https://api.github.com/repos/netty/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/netty/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/netty/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/netty/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/netty/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/netty/netty/languages","stargazers_url":"https://api.github.com/repos/netty/netty/stargazers","contributors_url":"https://api.github.com/repos/netty/netty/contributors","subscribers_url":"https://api.github.com/repos/netty/netty/subscribers","subscription_url":"https://api.github.com/repos/netty/netty/subscription","commits_url":"https://api.github.com/repos/netty/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/netty/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/netty/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/netty/netty/issues/comments/{number}","contents_url":"https://api.github.com/repos/netty/netty/contents/{+path}","compare_url":"https://api.github.com/repos/netty/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/netty/netty/merges","archive_url":"https://api.github.com/repos/netty/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/netty/netty/downloads","issues_url":"https://api.github.com/repos/netty/netty/issues{/number}","pulls_url":"https://api.github.com/repos/netty/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/netty/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/netty/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/netty/netty/labels{/name}","releases_url":"https://api.github.com/repos/netty/netty/releases{/id}","created_at":"2010-11-09T09:22:21Z","updated_at":"2015-02-13T11:15:55Z","pushed_at":"2015-02-13T08:35:19Z","git_url":"git://github.com/netty/netty.git","ssh_url":"git@github.com:netty/netty.git","clone_url":"https://github.com/netty/netty.git","svn_url":"https://github.com/netty/netty","homepage":"http://netty.io/","size":107783,"stargazers_count":4159,"watchers_count":4159,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2019,"mirror_url":null,"open_issues_count":228,"forks":2019,"open_issues":228,"watchers":4159,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/3381"},"html":{"href":"https://github.com/netty/netty/pull/3381"},"issue":{"href":"https://api.github.com/repos/netty/netty/issues/3381"},"comments":{"href":"https://api.github.com/repos/netty/netty/issues/3381/comments"},"review_comments":{"href":"https://api.github.com/repos/netty/netty/pulls/3381/comments"},"review_comment":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/netty/netty/pulls/3381/commits"},"statuses":{"href":"https://api.github.com/repos/netty/netty/statuses/2fd660276ecd80aa7ca63e1cd58cce440f11b0ca"}}}}