bulktree
欢迎走进有风的地方~
BlogJava
首页
新文章
新随笔
聚合
管理
posts - 51, comments - 192, trackbacks - 0
生产者消费者问题(以面包为例)
package
Thread;
public
class
ProducerConsumer
{
public
static
void
main(String[] args)
{
SynchronizedStack ss
=
new
SynchronizedStack();
Producer p
=
new
Producer(ss);
//
产生一个生产者
Consumer c
=
new
Consumer(ss);
//
产生一个消费者
new
Thread(p).start();
//
启动生产者线程
new
Thread(c).start();
//
启动消费者线程
}
}
class
Bread
//
定义生产面包
{
int
id;
Bread(
int
id)
{
this
.id
=
id;
}
public
String toString()
//
重写toString方法
{
return
"
bread :
"
+
id;
}
}
class
SynchronizedStack
//
定义一个盛面包的容器
{
int
index
=
0
;
Bread[] bread
=
new
Bread[
6
];
public
synchronized
void
putIn(Bread bread)
//
放进方法
{
while
(index
==
this
.bread.length)
{
try
{
this
.wait();
//
只针对synchronized
//
Object对象的wait()方法,表示调用当前对象的wait()方法,运行当前对象的此方法的线程等待,直到被notify()唤醒
}
catch
(InterruptedException e)
{
e.printStackTrace();
}
}
this
.notify();
//
唤醒一个wait的线程
//
this.notifyAll();
//
唤醒所有wait的线程
this
.bread[index]
=
bread;
index
++
;
}
public
synchronized
Bread putOut()
//
拿出方法
{
while
(index
==
0
)
{
try
{
this
.wait();
}
catch
(InterruptedException e)
{
e.printStackTrace();
}
}
this
.notify();
//
唤醒一个wait的线程
//
this.notifyAll();
//
唤醒所有wait的线程
index
--
;
return
bread[index];
}
}
class
Producer
implements
Runnable
{
SynchronizedStack ss
=
null
;
Producer(SynchronizedStack ss)
{
this
.ss
=
ss;
}
public
void
run()
{
for
(
int
i
=
0
;i
<
30
;i
++
)
{
Bread bread
=
new
Bread(i);
ss.putIn(bread);
System.out.println(
"
生产了 :
"
+
bread);
try
{
//
Thread.sleep(1000);
Thread.sleep((
int
)Math.random()
*
1000
);
}
catch
(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class
Consumer
implements
Runnable
{
SynchronizedStack ss
=
null
;
Consumer(SynchronizedStack ss)
{
this
.ss
=
ss;
}
public
void
run()
{
for
(
int
i
=
0
;i
<
30
;i
++
)
{
Bread bread
=
ss.putOut();
System.out.println(
"
消费了 :
"
+
bread);
try
{
//
Thread.sleep(1000);
Thread.sleep((
int
)Math.random()
*
1000
);
}
catch
(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
posted on 2007-12-10 19:48
凌晨风
阅读(1771)
评论(4)
编辑
收藏
所属分类:
Java学习笔记
FeedBack:
#
re: 生产者消费者问题(以面包为例)
2007-12-10 21:35 |
wǒ愛伱--咾婆
呵呵..支持了...好东东...写的不错啊..
回复
更多评论
#
re: 生产者消费者问题(以面包为例)
2007-12-11 00:28 |
zsulzm
这样貌似有问题吧
回复
更多评论
#
re: 生产者消费者问题(以面包为例)
2008-04-02 15:15 |
不行吧
吃面包的时候不能往里面放面包的。
回复
更多评论
#
re: 生产者消费者问题(以面包为例)[未登录]
2008-04-28 15:29 |
z
有点问题,输出固定不够明显.Consumer和Producer中不必都有 try
{
// Thread.sleep(1000);
Thread.sleep((int)Math.random() * 1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
去掉一个,就明显多了.
回复
更多评论
IT新闻
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关文章:
Struts2整合JasperReport预览HTML格式不显示图片问题解决方案
Log4j的使用
dom4j操作xml基础--Visitor访问模式解析XML
Visitor访问者模式---------学习dom4j时遇到的顺便拿来交流
office javaScript调试工具
java截取字符串的一些常用处理
JFreeChart简单使用
用commons-email-1.1.jar实现发邮件功能:
Struts2表单请求流程(二)— Struts2高级的POJO访问
Struts2表单请求流程(一)
<
2007年12月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
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
1
2
3
4
5
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(3)
给我留言
查看公开留言
查看私人留言
我参与的团队
分享.学习.交流团队(0/0)
西安java用户群(0/0)
深圳Java俱乐部(1/1)
随笔分类
DATABSE(1)
iReport + JasperReport 系列(11)
Java学习笔记(29)
Spring/Hibernate/Struts2(3)
操作系统的一些解决方案(2)
源码备份
生活感悟(2)
随笔档案
2008年12月 (12)
2008年8月 (6)
2008年4月 (4)
2008年3月 (1)
2008年1月 (1)
2007年12月 (20)
2007年11月 (6)
文章分类
系统的一些解决方案(1)
文章档案
2007年11月 (1)
新闻分类
Program Information
一些转载的经典(2)
新闻档案
2007年12月 (1)
2007年11月 (1)
收藏夹
CSS+JavaScript(1)
DATEBSE
J2EE/Jsp/Servlet
Java基础知识
Struts2
设计模式
最新随笔
1. iReport+jasperReport之图片控件
2. iReport+jasperReport之JFreeChart(图表报表)
3. iReport+jasperReport之scriptlet(续)
4. iReport+jasperReport之scriptlet
5. iReport+jasperReport之NoXML
6. iReport+jasperReport之CSV、XML数据源
7. iReport+jasperReport之BEAN数据源(续)
8. iReport+jasperReport之BEAN数据源
9. iReport+jasperReport之JDBC数据源
10. iReport+jasperReport概念的澄清(续)
搜索
最新评论
1. re: 数据库关系表解决方案
df
--df
2. re: iReport+jasperReport之JFreeChart(图表报表)
@shuaijie
你自己写啦。。要别人的代码!!
--ps
3. re: iReport+jasperReport之CSV、XML数据源
这个问题很好处理,查一下api,对这类型转化有说明的
--凌晨风
4. re: iReport+jasperReport之CSV、XML数据源[未登录]
评论内容较长,点击标题查看
--菜鸟
5. re: Spring2.5.3+Hibernate3.2+Struts2.0.11整合[未登录]
struts.objectFactory=spring源码中没有,好像不能跑吧!
--Harold.Zhang
阅读排行榜
1. Struts2中select/doubleselect标签数据显示(5525)
2. Spring2.5.3+Hibernate3.2+Struts2.0.11整合(4767)
3. JDK5增强for循环的使用(3607)
4. 去北京面试时的题目(老师生日)微软面试题(3398)
5. 用commons-email-1.1.jar实现发邮件功能:(3077)
评论排行榜
1. 去北京面试时的题目(老师生日)微软面试题(26)
2. iReport+jasperReport概念的澄清(续)(23)
3. JDK5增强for循环的使用(21)
4. Spring2.5.3+Hibernate3.2+Struts2.0.11整合(19)
5. 用commons-email-1.1.jar实现发邮件功能:(15)