Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
25288406
C
CS-Notes
项目概览
wushizhenking
/
CS-Notes
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
CS-Notes
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
25288406
编写于
4月 09, 2018
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
febc90c8
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
54 addition
and
2 deletion
+54
-2
notes/Java 并发.md
notes/Java 并发.md
+54
-2
pics/15b45dc6-27aa-4519-9194-f4acfa2b077f.jpg
pics/15b45dc6-27aa-4519-9194-f4acfa2b077f.jpg
+0
-0
未找到文件。
notes/Java 并发.md
浏览文件 @
25288406
...
...
@@ -927,11 +927,61 @@ public class ProducerConsumer {
produce..produce..consume..consume..produce..consume..produce..consume..produce..consume..
```
## ForkJoin
// TODO
主要用于并行计算中,和 MapReduce 原理类似,都是把大的计算任务拆分成多个小任务并行计算。
```
java
public
class
ForkJoinExample
extends
RecursiveTask
<
Integer
>
{
private
final
int
threhold
=
5
;
private
int
first
;
private
int
last
;
public
ForkJoinExample
(
int
first
,
int
last
)
{
this
.
first
=
first
;
this
.
last
=
last
;
}
@Override
protected
Integer
compute
()
{
int
result
=
0
;
if
(
last
-
first
<=
threhold
)
{
// 任务足够小则直接计算
for
(
int
i
=
first
;
i
<=
last
;
i
++)
{
result
+=
i
;
}
}
else
{
// 拆分成小任务
int
middle
=
first
+
(
last
-
first
)
/
2
;
ForkJoinExample
leftTask
=
new
ForkJoinExample
(
first
,
middle
);
ForkJoinExample
rightTask
=
new
ForkJoinExample
(
middle
+
1
,
last
);
leftTask
.
fork
();
rightTask
.
fork
();
result
=
leftTask
.
join
()
+
rightTask
.
join
();
}
return
result
;
}
}
```
```
java
public
static
void
main
(
String
[]
args
)
throws
ExecutionException
,
InterruptedException
{
ForkJoinExample
example
=
new
ForkJoinExample
(
1
,
10000
);
ForkJoinPool
forkJoinPool
=
new
ForkJoinPool
();
Future
result
=
forkJoinPool
.
submit
(
example
);
System
.
out
.
println
(
result
.
get
());
}
```
ForkJoin 使用 ForkJoinPool 来启动,它是一个特殊的线程池,线程数量取决于 CPU 核数。
```
java
public
class
ForkJoinPool
extends
AbstractExecutorService
```
ForkJoinPool 实现了工作窃取算法来提高 CPU 的利用率。每个线程都维护了一个双端队列,用来存储需要执行的任务。工作窃取算法允许空闲的线程从其它线程的双端队列中窃取一个任务来执行。窃取的任务必须是最晚的任务,避免和队列所属线程发生竞争。例如下图中,Thread2 从 Thread1 的队列中拿出最晚的 Task1 任务,Thread1 会拿出 Task2 来执行,这样就避免发生竞争。但是如果队列中只有一个任务时还是会发生竞争。
<div
align=
"center"
>
<img
src=
"../pics//15b45dc6-27aa-4519-9194-f4acfa2b077f.jpg"
/>
</div><br>
# 九、线程不安全示例
...
...
@@ -1593,3 +1643,5 @@ public static String concatString(String s1, String s2, String s3) {
-
[
6장 Thread Synchronization
](
https://www.slideshare.net/novathinker/6-thread-synchronization
)
-
[
How is Java's ThreadLocal implemented under the hood?
](
https://stackoverflow.com/questions/1202444/how-is-javas-threadlocal-implemented-under-the-hood/15653015
)
-
[
Concurrent
](
https://sites.google.com/site/webdevelopart/21-compile/06-java/javase/concurrent?tmpl=%2Fsystem%2Fapp%2Ftemplates%2Fprint%2F&showPrintDialog=1
)
-
[
JAVA FORK JOIN EXAMPLE
](
http://www.javacreed.com/java-fork-join-example/
"Java Fork Join Example"
)
-
[
聊聊并发(八)——Fork/Join 框架介绍
](
http://ifeve.com/talk-concurrency-forkjoin/
)
pics/15b45dc6-27aa-4519-9194-f4acfa2b077f.jpg
0 → 100644
浏览文件 @
25288406
44.0 KB
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录