Java多线程设计模式(1)
来源:互联网 发布:热血仙境源码一键端 编辑:程序博客网 时间:2024/04/29 12:52
2013-05-16 14:10:13
标签:多线程 设计模式 synchronized
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://computerdragon.blog.51cto.com/6235984/1201769
2013-05-17 14:49:08
标签:java 生产者与消费者模式 多线程
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://computerdragon.blog.51cto.com/6235984/1202679
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
package
whut.producer;
import
java.util.Random;
public
class
MakerThread
extends
Thread{
private
final
Table table;
private
final
Random random;
private
static
int
id=
0
;
public
MakerThread(String name,Table table,
long
seed)
{
super
(name);
this
.table=table;
this
.random=
new
Random(seed);
}
public
void
run()
{
try
{
while
(
true
)
{
Thread.sleep(random.nextInt(
1000
));
String cake=
" [Cake No."
+nextId()+
" by "
+Thread.currentThread().getName()+
"]"
;
table.put(cake);
}
}
catch
(InterruptedException e)
{
}
}
//为了使得所有实例共享该字段
public
static
synchronized
int
nextId()
{
return
id++;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package
whut.producer;
import
java.util.Random;
public
class
EaterThread
extends
Thread{
private
final
Table table;
private
final
Random random;
public
EaterThread(String name,Table table,
long
seed)
{
super
(name);
this
.table=table;
this
.random=
new
Random(seed);
}
public
void
run()
{
try
{
while
(
true
)
{
String cake=table.take();
Thread.sleep(random.nextInt(
1000
));
}
}
catch
(InterruptedException e)
{
}
}
}
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
package
whut.producer;
public
class
Table {
private
final
String[] cakes;
//利用数组来作为缓冲区
private
int
head;
//下一次蛋糕取的位置
private
int
tail;
//下一次蛋糕放置位置
private
int
count;
//桌子上蛋糕的总数
public
Table(
int
count)
{
this
.cakes=
new
String[count];
this
.head=
0
;
this
.tail=
0
;
this
.count=
0
;
}
public
synchronized
void
put(String cake)
throws
InterruptedException
{
System.out.println(Thread.currentThread().getName()+
" puts "
+cake);
while
(count>=cakes.length)
{
System.out.println(Thread.currentThread().getName()+
" Begin wait...."
);
wait();
System.out.println(Thread.currentThread().getName()+
" End wait...."
);
}
cakes[tail]=cake;
tail=(tail+
1
)%cakes.length;
count++;
notifyAll();
}
//取蛋糕
public
synchronized
String take()
throws
InterruptedException
{
while
(count<=
0
)
{
System.out.println(Thread.currentThread().getName()+
" Begin wait...."
);
wait();
System.out.println(Thread.currentThread().getName()+
" End wait...."
);
}
String cake=cakes[head];
head=(head+
1
)%cakes.length;
count--;
notifyAll();
System.out.println(Thread.currentThread().getName()+
" gets "
+cake);
return
cake;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package
whut.producer;
public
class
MainTest {
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
Table table=
new
Table(
3
);
new
MakerThread(
"MakerThread-1"
,table,
31415
).start();
new
MakerThread(
"MakerThread-2"
,table,
92653
).start();
new
MakerThread(
"MakerThread-3"
,table,
58979
).start();
new
EaterThread(
"EaterThread-1"
,table,
32384
).start();
new
EaterThread(
"EaterThread-2"
,table,
32384
).start();
new
EaterThread(
"EaterThread-3"
,table,
32384
).start();
//可以通过调用interrupt来去中断结束任何线程
}
}
- java多线程设计模式(1)
- Java多线程设计模式(1)
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- java多线程设计模式
- Java多线程设计模式
- java多线程设计模式
- Java多线程设计模式
- java多线程设计模式
- js removeChild 障眼法 可能出现的错误
- 书:深入理解计算机系统(P651) 之 并发编程:用I/O多路复用的实现服务器
- wpf中控件字体大小智能随着内容长度的变化也变化。
- 写给大家看的web设计书
- jsp的四个scope范围大小
- Java多线程设计模式(1)
- Windows下,实现不使用缓存的文件读写方式
- 项目代码风格要求(转自博客园-张子阳)
- 编码风格不是编码规范
- 使用django从数据库中随机取N条记录的不同方法及其性能实测
- 基于visual Studio2013解决C语言竞赛题之0605strcat
- Linux C ini文件工具类
- 一次涉及两个大表关联的优化
- 字符串操作 c