{"action":"created","comment":{"url":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/comments/73803375","id":73803375,"diff_hunk":"@@ -0,0 +1,727 @@\n+转载请附原文链接：[ThreadPoolExecutor源码学习笔记](http://extremej.itscoder.com/threadpoolexecutor_source/)\n+\n+**大部分分析以注释形式写在源码中**\n+\n+本篇笔记将从 ThreadPoolExecutor 的一次使用上来分析源码，主要涉及线程池创建，execute 的步骤，任务添加到阻塞队列，线程从阻塞队列中拿取任务执行，线程的回收，线程池的终止。\n+\n+涉及到的类有\n+\n+- Executors — 获取线程池\n+\n+- ThreadPoolExecutor — 线程池\n+\n+- Worker — 工作线程\n+\n+- LinkedBlockingQueue — 阻塞队列\n+\n+- RejectedExecutionHandler — 任务拒绝处理器（实在不知道什么翻译～）\n+\n+  ​\n+\n+### 线程池的获取\n+\n+我们知道可以通过 Executors 来获取不同类型的线程池，那么就从 Executors 来开始看它是如何返回不同类型的线程池的，看看我们常用的一些方法\n+\n+```java\n+//获取一个固定线程池\n+public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {\n+    return new ThreadPoolExecutor(nThreads, nThreads,\n+                                  0L, TimeUnit.MILLISECONDS,\n+                                  new LinkedBlockingQueue<Runnable>(),\n+                                  threadFactory);\n+}\n+//获取只有一个线程的池子\n+public static ExecutorService newSingleThreadExecutor() {\n+        return new FinalizableDelegatedExecutorService\n+            (new ThreadPoolExecutor(1, 1,\n+                                    0L, TimeUnit.MILLISECONDS,\n+                                    new LinkedBlockingQueue<Runnable>()));\n+    }\n+//获取一个缓存线程池，可变\n+public static ExecutorService newCachedThreadPool() {\n+        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,\n+                                      60L, TimeUnit.SECONDS,\n+                                      new SynchronousQueue<Runnable>());\n+    }\n+```\n+\n+从上面的三个方法可以发现其实都是 new 了一个 ThreadPoolExecutor ，但是传入的参数不同，我们进到这个构造方法中去一探究竟，看看不同的参数到底代表了什么\n+\n+```java\n+//构造方法\n+public ThreadPoolExecutor(int corePoolSize,\n+                          int maximumPoolSize,\n+                          long keepAliveTime,\n+                          TimeUnit unit,\n+                          BlockingQueue<Runnable> workQueue,\n+                          ThreadFactory threadFactory,\n+                          RejectedExecutionHandler handler) {\n+    if (corePoolSize < 0 ||\n+        maximumPoolSize <= 0 ||\n+        maximumPoolSize < corePoolSize ||\n+        keepAliveTime < 0)\n+        throw new IllegalArgumentException();\n+    if (workQueue == null || threadFactory == null || handler == null)\n+        throw new NullPointerException();\n+    this.corePoolSize = corePoolSize;\n+    this.maximumPoolSize = maximumPoolSize;\n+    this.workQueue = workQueue;\n+    this.keepAliveTime = unit.toNanos(keepAliveTime);\n+    this.threadFactory = threadFactory;\n+    this.handler = handler;\n+}\n+```\n+\n+通过参数名称以及注释可以知道这几个参数的作用分别是\n+\n+- corePoolSize — 核心线程数，即允许闲置的线程数目\n+- maximumPoolSize — 最大线程数，即这个线程池的容量\n+- keepAliveTime — 非核心线程的闲置存活时间","path":"phase_1_20160807/joe_20160724_threadpoolexecutor_source_code_ analysis.md","position":77,"original_position":79,"commit_id":"093c423cfcd2728b5d5378329509c73fc7ecbe53","original_commit_id":"093c423cfcd2728b5d5378329509c73fc7ecbe53","user":{"login":"yongyu0102","id":13392987,"avatar_url":"https://avatars.githubusercontent.com/u/13392987?v=3","gravatar_id":"","url":"https://api.github.com/users/yongyu0102","html_url":"https://github.com/yongyu0102","followers_url":"https://api.github.com/users/yongyu0102/followers","following_url":"https://api.github.com/users/yongyu0102/following{/other_user}","gists_url":"https://api.github.com/users/yongyu0102/gists{/gist_id}","starred_url":"https://api.github.com/users/yongyu0102/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yongyu0102/subscriptions","organizations_url":"https://api.github.com/users/yongyu0102/orgs","repos_url":"https://api.github.com/users/yongyu0102/repos","events_url":"https://api.github.com/users/yongyu0102/events{/privacy}","received_events_url":"https://api.github.com/users/yongyu0102/received_events","type":"User","site_admin":false},"body":"“keepAliveTime — 非核心线程的闲置存活时间 ” 这么说感觉不准确阿风，当设定allowCoreThreadTimeOut=true的时候，核心线程也应该受keepAliveTime 限制吧","created_at":"2016-08-07T14:59:33Z","updated_at":"2016-08-07T14:59:33Z","html_url":"https://github.com/itsCoder/weeklyblog/pull/11#discussion_r73803375","pull_request_url":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/11","_links":{"self":{"href":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/comments/73803375"},"html":{"href":"https://github.com/itsCoder/weeklyblog/pull/11#discussion_r73803375"},"pull_request":{"href":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/11"}}},"pull_request":{"url":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/11","id":79580753,"html_url":"https://github.com/itsCoder/weeklyblog/pull/11","diff_url":"https://github.com/itsCoder/weeklyblog/pull/11.diff","patch_url":"https://github.com/itsCoder/weeklyblog/pull/11.patch","issue_url":"https://api.github.com/repos/itsCoder/weeklyblog/issues/11","number":11,"state":"open","locked":false,"title":"第一期 ThreadPoolExecutor源码分析 by joe","user":{"login":"JoeSteven","id":16322233,"avatar_url":"https://avatars.githubusercontent.com/u/16322233?v=3","gravatar_id":"","url":"https://api.github.com/users/JoeSteven","html_url":"https://github.com/JoeSteven","followers_url":"https://api.github.com/users/JoeSteven/followers","following_url":"https://api.github.com/users/JoeSteven/following{/other_user}","gists_url":"https://api.github.com/users/JoeSteven/gists{/gist_id}","starred_url":"https://api.github.com/users/JoeSteven/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JoeSteven/subscriptions","organizations_url":"https://api.github.com/users/JoeSteven/orgs","repos_url":"https://api.github.com/users/JoeSteven/repos","events_url":"https://api.github.com/users/JoeSteven/events{/privacy}","received_events_url":"https://api.github.com/users/JoeSteven/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-08-01T14:49:08Z","updated_at":"2016-08-07T14:59:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"83c92c63fab30d6d8e4d1aa3e5e90cb3829fccf3","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/11/commits","review_comments_url":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/11/comments","review_comment_url":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/comments{/number}","comments_url":"https://api.github.com/repos/itsCoder/weeklyblog/issues/11/comments","statuses_url":"https://api.github.com/repos/itsCoder/weeklyblog/statuses/093c423cfcd2728b5d5378329509c73fc7ecbe53","head":{"label":"itsCoder:member/joe","ref":"member/joe","sha":"093c423cfcd2728b5d5378329509c73fc7ecbe53","user":{"login":"itsCoder","id":20601570,"avatar_url":"https://avatars.githubusercontent.com/u/20601570?v=3","gravatar_id":"","url":"https://api.github.com/users/itsCoder","html_url":"https://github.com/itsCoder","followers_url":"https://api.github.com/users/itsCoder/followers","following_url":"https://api.github.com/users/itsCoder/following{/other_user}","gists_url":"https://api.github.com/users/itsCoder/gists{/gist_id}","starred_url":"https://api.github.com/users/itsCoder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/itsCoder/subscriptions","organizations_url":"https://api.github.com/users/itsCoder/orgs","repos_url":"https://api.github.com/users/itsCoder/repos","events_url":"https://api.github.com/users/itsCoder/events{/privacy}","received_events_url":"https://api.github.com/users/itsCoder/received_events","type":"Organization","site_admin":false},"repo":{"id":63970822,"name":"weeklyblog","full_name":"itsCoder/weeklyblog","owner":{"login":"itsCoder","id":20601570,"avatar_url":"https://avatars.githubusercontent.com/u/20601570?v=3","gravatar_id":"","url":"https://api.github.com/users/itsCoder","html_url":"https://github.com/itsCoder","followers_url":"https://api.github.com/users/itsCoder/followers","following_url":"https://api.github.com/users/itsCoder/following{/other_user}","gists_url":"https://api.github.com/users/itsCoder/gists{/gist_id}","starred_url":"https://api.github.com/users/itsCoder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/itsCoder/subscriptions","organizations_url":"https://api.github.com/users/itsCoder/orgs","repos_url":"https://api.github.com/users/itsCoder/repos","events_url":"https://api.github.com/users/itsCoder/events{/privacy}","received_events_url":"https://api.github.com/users/itsCoder/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/itsCoder/weeklyblog","description":null,"fork":false,"url":"https://api.github.com/repos/itsCoder/weeklyblog","forks_url":"https://api.github.com/repos/itsCoder/weeklyblog/forks","keys_url":"https://api.github.com/repos/itsCoder/weeklyblog/keys{/key_id}","collaborators_url":"https://api.github.com/repos/itsCoder/weeklyblog/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/itsCoder/weeklyblog/teams","hooks_url":"https://api.github.com/repos/itsCoder/weeklyblog/hooks","issue_events_url":"https://api.github.com/repos/itsCoder/weeklyblog/issues/events{/number}","events_url":"https://api.github.com/repos/itsCoder/weeklyblog/events","assignees_url":"https://api.github.com/repos/itsCoder/weeklyblog/assignees{/user}","branches_url":"https://api.github.com/repos/itsCoder/weeklyblog/branches{/branch}","tags_url":"https://api.github.com/repos/itsCoder/weeklyblog/tags","blobs_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/refs{/sha}","trees_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/trees{/sha}","statuses_url":"https://api.github.com/repos/itsCoder/weeklyblog/statuses/{sha}","languages_url":"https://api.github.com/repos/itsCoder/weeklyblog/languages","stargazers_url":"https://api.github.com/repos/itsCoder/weeklyblog/stargazers","contributors_url":"https://api.github.com/repos/itsCoder/weeklyblog/contributors","subscribers_url":"https://api.github.com/repos/itsCoder/weeklyblog/subscribers","subscription_url":"https://api.github.com/repos/itsCoder/weeklyblog/subscription","commits_url":"https://api.github.com/repos/itsCoder/weeklyblog/commits{/sha}","git_commits_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/commits{/sha}","comments_url":"https://api.github.com/repos/itsCoder/weeklyblog/comments{/number}","issue_comment_url":"https://api.github.com/repos/itsCoder/weeklyblog/issues/comments{/number}","contents_url":"https://api.github.com/repos/itsCoder/weeklyblog/contents/{+path}","compare_url":"https://api.github.com/repos/itsCoder/weeklyblog/compare/{base}...{head}","merges_url":"https://api.github.com/repos/itsCoder/weeklyblog/merges","archive_url":"https://api.github.com/repos/itsCoder/weeklyblog/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/itsCoder/weeklyblog/downloads","issues_url":"https://api.github.com/repos/itsCoder/weeklyblog/issues{/number}","pulls_url":"https://api.github.com/repos/itsCoder/weeklyblog/pulls{/number}","milestones_url":"https://api.github.com/repos/itsCoder/weeklyblog/milestones{/number}","notifications_url":"https://api.github.com/repos/itsCoder/weeklyblog/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/itsCoder/weeklyblog/labels{/name}","releases_url":"https://api.github.com/repos/itsCoder/weeklyblog/releases{/id}","deployments_url":"https://api.github.com/repos/itsCoder/weeklyblog/deployments","created_at":"2016-07-22T17:20:08Z","updated_at":"2016-08-01T09:39:27Z","pushed_at":"2016-08-07T14:57:53Z","git_url":"git://github.com/itsCoder/weeklyblog.git","ssh_url":"git@github.com:itsCoder/weeklyblog.git","clone_url":"https://github.com/itsCoder/weeklyblog.git","svn_url":"https://github.com/itsCoder/weeklyblog","homepage":null,"size":1695,"stargazers_count":14,"watchers_count":14,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":10,"forks":2,"open_issues":10,"watchers":14,"default_branch":"master"}},"base":{"label":"itsCoder:dev","ref":"dev","sha":"b583cf9aa1fd8b6ebe55e88534e8edd78970eeec","user":{"login":"itsCoder","id":20601570,"avatar_url":"https://avatars.githubusercontent.com/u/20601570?v=3","gravatar_id":"","url":"https://api.github.com/users/itsCoder","html_url":"https://github.com/itsCoder","followers_url":"https://api.github.com/users/itsCoder/followers","following_url":"https://api.github.com/users/itsCoder/following{/other_user}","gists_url":"https://api.github.com/users/itsCoder/gists{/gist_id}","starred_url":"https://api.github.com/users/itsCoder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/itsCoder/subscriptions","organizations_url":"https://api.github.com/users/itsCoder/orgs","repos_url":"https://api.github.com/users/itsCoder/repos","events_url":"https://api.github.com/users/itsCoder/events{/privacy}","received_events_url":"https://api.github.com/users/itsCoder/received_events","type":"Organization","site_admin":false},"repo":{"id":63970822,"name":"weeklyblog","full_name":"itsCoder/weeklyblog","owner":{"login":"itsCoder","id":20601570,"avatar_url":"https://avatars.githubusercontent.com/u/20601570?v=3","gravatar_id":"","url":"https://api.github.com/users/itsCoder","html_url":"https://github.com/itsCoder","followers_url":"https://api.github.com/users/itsCoder/followers","following_url":"https://api.github.com/users/itsCoder/following{/other_user}","gists_url":"https://api.github.com/users/itsCoder/gists{/gist_id}","starred_url":"https://api.github.com/users/itsCoder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/itsCoder/subscriptions","organizations_url":"https://api.github.com/users/itsCoder/orgs","repos_url":"https://api.github.com/users/itsCoder/repos","events_url":"https://api.github.com/users/itsCoder/events{/privacy}","received_events_url":"https://api.github.com/users/itsCoder/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/itsCoder/weeklyblog","description":null,"fork":false,"url":"https://api.github.com/repos/itsCoder/weeklyblog","forks_url":"https://api.github.com/repos/itsCoder/weeklyblog/forks","keys_url":"https://api.github.com/repos/itsCoder/weeklyblog/keys{/key_id}","collaborators_url":"https://api.github.com/repos/itsCoder/weeklyblog/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/itsCoder/weeklyblog/teams","hooks_url":"https://api.github.com/repos/itsCoder/weeklyblog/hooks","issue_events_url":"https://api.github.com/repos/itsCoder/weeklyblog/issues/events{/number}","events_url":"https://api.github.com/repos/itsCoder/weeklyblog/events","assignees_url":"https://api.github.com/repos/itsCoder/weeklyblog/assignees{/user}","branches_url":"https://api.github.com/repos/itsCoder/weeklyblog/branches{/branch}","tags_url":"https://api.github.com/repos/itsCoder/weeklyblog/tags","blobs_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/refs{/sha}","trees_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/trees{/sha}","statuses_url":"https://api.github.com/repos/itsCoder/weeklyblog/statuses/{sha}","languages_url":"https://api.github.com/repos/itsCoder/weeklyblog/languages","stargazers_url":"https://api.github.com/repos/itsCoder/weeklyblog/stargazers","contributors_url":"https://api.github.com/repos/itsCoder/weeklyblog/contributors","subscribers_url":"https://api.github.com/repos/itsCoder/weeklyblog/subscribers","subscription_url":"https://api.github.com/repos/itsCoder/weeklyblog/subscription","commits_url":"https://api.github.com/repos/itsCoder/weeklyblog/commits{/sha}","git_commits_url":"https://api.github.com/repos/itsCoder/weeklyblog/git/commits{/sha}","comments_url":"https://api.github.com/repos/itsCoder/weeklyblog/comments{/number}","issue_comment_url":"https://api.github.com/repos/itsCoder/weeklyblog/issues/comments{/number}","contents_url":"https://api.github.com/repos/itsCoder/weeklyblog/contents/{+path}","compare_url":"https://api.github.com/repos/itsCoder/weeklyblog/compare/{base}...{head}","merges_url":"https://api.github.com/repos/itsCoder/weeklyblog/merges","archive_url":"https://api.github.com/repos/itsCoder/weeklyblog/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/itsCoder/weeklyblog/downloads","issues_url":"https://api.github.com/repos/itsCoder/weeklyblog/issues{/number}","pulls_url":"https://api.github.com/repos/itsCoder/weeklyblog/pulls{/number}","milestones_url":"https://api.github.com/repos/itsCoder/weeklyblog/milestones{/number}","notifications_url":"https://api.github.com/repos/itsCoder/weeklyblog/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/itsCoder/weeklyblog/labels{/name}","releases_url":"https://api.github.com/repos/itsCoder/weeklyblog/releases{/id}","deployments_url":"https://api.github.com/repos/itsCoder/weeklyblog/deployments","created_at":"2016-07-22T17:20:08Z","updated_at":"2016-08-01T09:39:27Z","pushed_at":"2016-08-07T14:57:53Z","git_url":"git://github.com/itsCoder/weeklyblog.git","ssh_url":"git@github.com:itsCoder/weeklyblog.git","clone_url":"https://github.com/itsCoder/weeklyblog.git","svn_url":"https://github.com/itsCoder/weeklyblog","homepage":null,"size":1695,"stargazers_count":14,"watchers_count":14,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":10,"forks":2,"open_issues":10,"watchers":14,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/11"},"html":{"href":"https://github.com/itsCoder/weeklyblog/pull/11"},"issue":{"href":"https://api.github.com/repos/itsCoder/weeklyblog/issues/11"},"comments":{"href":"https://api.github.com/repos/itsCoder/weeklyblog/issues/11/comments"},"review_comments":{"href":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/11/comments"},"review_comment":{"href":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/itsCoder/weeklyblog/pulls/11/commits"},"statuses":{"href":"https://api.github.com/repos/itsCoder/weeklyblog/statuses/093c423cfcd2728b5d5378329509c73fc7ecbe53"}}}}
{"actor":{"display_login":"yongyu0102"}}