Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
JavaDevTeam
notes
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
notes
/
Java
/
concurrent
/
java-juc-ExecutorService.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
58 lines (44 loc) · 1.98 KB
master
Breadcrumbs
notes
/
Java
/
concurrent
/
java-juc-ExecutorService.java
Copy path
Top
File metadata and controls
Code
Blame
58 lines (44 loc) · 1.98 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---------------------------
ExecutorService
|
---------------------------
#
线程池工具
#
体系
Executor
(
接口
)
|-
ExecutorService
(
接口
)
|-
ThreadPoolExecutor
(
实现
)
|-
ScheduledExecutorService
(
接口
,
负责线程调度
)
|-
ScheduledThreadPoolExecutor
(
继承了ThreadPoolExecutor又实现了ScheduledExecutorService
)
#
创建
ExecutorService
pool
=
Executors
.
newFixedThreadPool
(
2
);
*
创建一个可重用固定线程数的线程池
,
以共享的无界队列方式来运行这些线程
。
ExecutorService
pool
=
Executors
.
newCachedThreadPool
();
*
创建一个可根据需要创建新线程的线程池
,
但是在以前构造的线程可用时将重用它们
。
*
对于执行很多短期异步任务的程序而言
,
这些线程池通常可提高程序性能
。
*
线程数量不固定
,
可以根据需求更新线程数量
ExecutorService
pool
=
Executors
.
newSingleThreadExecutor
()
*
创建一个使用单个
worker
线程的
Executor
,
以无界队列方式来运行该线程
。
*
里面只有一个线程
ScheduledExecutorService
es
=
Executors
.
newScheduledThreadPool
(
10
);
*
创建调度线程池
*
创建固定大小的线程池
,
可以延时
/
重复的执行任务调度
#
使用线程执行任务
Future
<?>
submit
(
Runnable
task
);
<
T
>
Future
<
T
>
submit
(
Callable
<
T
>
task
);
void
execute
(
Runnable
command
);
*
execute和submit的区别
1.
接收的参数不一样
;
2.
submit
()
有返回值
,
而execute
()
没有
;
例如
,
有个validation的task
,
希望该task执行完后告诉我它的执行结果
,
是成功还是失败
,
然后继续下面的操作
。
3.
submit
()
可以进行Exception处理
;
例如
,
如果task里会抛出checked或者unchecked
exception
,
而你又希望外面的调用者能够感知这些exception并做出及时的处理
,
那么就需要用到submit
,
通过对Future
.
get
()
进行抛出异常的捕获
,
然后对其进行处理
。
#
线程池调度
ScheduledExecutorService
es
=
Executors
.
newScheduledThreadPool
(
10
);
ScheduledFuture
<?>
schedule
(
Runnable
command
,
long
delay
,
TimeUnit
unit
);
*
在指定延迟后
,
执行command
<
V
>
ScheduledFuture
<
V
>
schedule
(
Callable
<
V
>
callable
,
long
delay
,
TimeUnit
unit
);
*
在指定延迟后
,
执行callable
#
关闭线程池
void
shutdown
();
*
会等到所有任务完成才会关闭
List
<
Runnable
>
shutdownNow
();
*
立即关闭线程池
You can’t perform that action at this time.