Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
37cf410c
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,发现更多精彩内容 >>
提交
37cf410c
编写于
4月 09, 2018
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
bd183c28
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
30 addition
and
55 deletion
+30
-55
notes/Java 并发.md
notes/Java 并发.md
+30
-55
未找到文件。
notes/Java 并发.md
浏览文件 @
37cf410c
...
...
@@ -813,7 +813,7 @@ public class SemaphoreExample {
## FutureTask
在介绍 Callable 时我们知道它可以有返回值,返回值通过 Future 进行封装。FutureTask 实现了 RunnableFuture 接口,该接口继承自 Runnable 和 Future
<V>
接口,这使得 FutureTask 既可以当做一个任务执行,也可以有返回值。
在介绍 Callable 时我们知道它可以有返回值,返回值通过 Future
<V>
进行封装。FutureTask 实现了 RunnableFuture 接口,该接口继承自 Runnable 和 Future
<V>
接口,这使得 FutureTask 既可以当做一个任务执行,也可以有返回值。
```
java
public
class
FutureTask
<
V
>
implements
RunnableFuture
<
V
>
...
...
@@ -823,7 +823,7 @@ public class FutureTask<V> implements RunnableFuture<V>
public
interface
RunnableFuture
<
V
>
extends
Runnable
,
Future
<
V
>
```
当一个计算任务需要执行很长时间,那么就可以用 FutureTask 来封装这个任务,用一个线程去执行该任务,然后执行其它任务。当需要该任务的计算结果时,再通过 FutureTask 的 get() 方法获取。
当一个计算任务需要执行很长时间,那么就可以用 FutureTask 来封装这个任务,用一个线程去执行该任务,然后
其它线程继续
执行其它任务。当需要该任务的计算结果时,再通过 FutureTask 的 get() 方法获取。
```
java
public
class
FutureTaskExample
{
...
...
@@ -874,79 +874,54 @@ java.util.concurrent.BlockingQueue 接口有以下阻塞队列的实现:
**使用 BlockingQueue 实现生产者消费者问题**
```
java
// 生产者
public
class
Producer
implements
Runnable
{
private
BlockingQueue
<
String
>
queue
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.BlockingQueue
;
public
Producer
(
BlockingQueue
<
String
>
queue
)
{
this
.
queue
=
queue
;
}
public
class
ProducerConsumer
{
@Override
public
void
run
()
{
System
.
out
.
println
(
Thread
.
currentThread
().
getName
()
+
" is making product."
);
String
product
=
"Made By "
+
Thread
.
currentThread
().
getName
();
try
{
queue
.
put
(
product
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
private
static
BlockingQueue
<
String
>
queue
=
new
ArrayBlockingQueue
<>(
5
);
private
static
class
Producer
extends
Thread
{
@Override
public
void
run
()
{
queue
.
add
(
"product"
);
System
.
out
.
print
(
"produce.."
);
}
}
}
```
```
java
// 消费者
public
class
Consumer
implements
Runnable
{
private
BlockingQueue
<
String
>
queue
;
private
static
class
Consumer
extends
Thread
{
public
Consumer
(
BlockingQueue
<
String
>
queue
)
{
this
.
queue
=
queue
;
}
@Override
public
void
run
()
{
try
{
String
product
=
queue
.
take
();
System
.
out
.
println
(
Thread
.
currentThread
().
getName
()
+
" is consuming product."
+
"( "
+
product
+
" )"
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
@Override
public
void
run
()
{
try
{
queue
.
take
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
print
(
"consume.."
);
}
}
}
```
```
java
// 客户端
public
class
Client
{
public
static
void
main
(
String
[]
args
)
{
BlockingQueue
<
String
>
queue
=
new
LinkedBlockingQueue
<>(
5
);
for
(
int
i
=
0
;
i
<
2
;
i
++)
{
new
Thread
(
new
Consumer
(
queue
),
"Consumer-"
+
i
).
start
();
Producer
producer
=
new
Producer
();
producer
.
start
();
}
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
// 只有两个 Product,因此只能消费两个,其它三个消费者被阻塞
new
Thread
(
new
Producer
(
queue
),
"Producer-"
+
i
)
.
start
();
Consumer
consumer
=
new
Consumer
();
consumer
.
start
();
}
for
(
int
i
=
2
;
i
<
5
;
i
++)
{
new
Thread
(
new
Consumer
(
queue
),
"Consumer-"
+
i
).
start
();
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
Producer
producer
=
new
Producer
();
producer
.
start
();
}
}
}
```
```
html
// 运行结果
Producer-0 is making product.
Consumer-0 is consuming product.( Made By Producer-0 )
Producer-1 is making product.
Consumer-1 is consuming product.( Made By Producer-1 )
Producer-2 is making product.
Producer-3 is making product.
Producer-4 is making product.
Consumer-2 is consuming product.( Made By Producer-2 )
Consumer-3 is consuming product.( Made By Producer-3 )
Consumer-4 is consuming product.( Made By Producer-4 )
produce..produce..consume..consume..produce..consume..produce..consume..produce..consume..
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录