class Info{
private String name="徐彦"; //名称
private String content="程序员"; //内容
private boolean flag=false; //设置标志位
public synchronized void set(String name,String content){
if (!flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.setName(name); //设置名称
try{
Thread.sleep(100); //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
this.setContent(content); //设置内容
flag=false;
super.notify();
}
public synchronized void get( ){
if (flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(100); //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(this.getName()+"-->"+this.getContent());
flag=true;
super.notify();
}
public void setName(String name){ //设置名称
this.name=name;
}
public void setContent(String content){ //设置内容
this.content=content;
}
public String getName(){ //取得名称
return this.name;
}
public String getContent(){ //取得内容
return this.content;
}
}
class Producer implements Runnable{ //继承Runnable接口
private Info info=null; //声明info
//声明flag
public Producer(Info info){ //通过构造方法设置info
this.info=info;
}
public void run(){ //覆写run()方法
boolean flag=false;
for (int i=0;i<50 ;i++ ){
if (flag){
this.info.set("徐彦","程序员");
flag=false;
}
else{
this.info.set("希哲","教师");
flag=true;
}
}
} }
class Consumer implements Runnable{ //继承Runnable接口
private Info info=null; //声明info
public Consumer(Info info){
this.info=info;
}
public void run(){ //覆写run()
for (int i=0;i<50 ;i++ ){
this.info.get();
}
}
}
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info=new Info(); //实例化info
Producer pro=new Producer(info); //生产者
Consumer con=new Consumer(info); //消费者
new Thread(pro).start();
new Thread(con).start();
}
}
一个生产者与消费者的Demo,小弟有点迷糊,感觉有点明白,但是又不能很清楚,希望哪位大侠现身,解决下啊,小弟在线等啊,谢了 啊 !!!!
private String name="徐彦"; //名称
private String content="程序员"; //内容
private boolean flag=false; //设置标志位
public synchronized void set(String name,String content){
if (!flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.setName(name); //设置名称
try{
Thread.sleep(100); //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
this.setContent(content); //设置内容
flag=false;
super.notify();
}
public synchronized void get( ){
if (flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(100); //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(this.getName()+"-->"+this.getContent());
flag=true;
super.notify();
}
public void setName(String name){ //设置名称
this.name=name;
}
public void setContent(String content){ //设置内容
this.content=content;
}
public String getName(){ //取得名称
return this.name;
}
public String getContent(){ //取得内容
return this.content;
}
}
class Producer implements Runnable{ //继承Runnable接口
private Info info=null; //声明info
//声明flag
public Producer(Info info){ //通过构造方法设置info
this.info=info;
}
public void run(){ //覆写run()方法
boolean flag=false;
for (int i=0;i<50 ;i++ ){
if (flag){
this.info.set("徐彦","程序员");
flag=false;
}
else{
this.info.set("希哲","教师");
flag=true;
}
}
} }
class Consumer implements Runnable{ //继承Runnable接口
private Info info=null; //声明info
public Consumer(Info info){
this.info=info;
}
public void run(){ //覆写run()
for (int i=0;i<50 ;i++ ){
this.info.get();
}
}
}
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info=new Info(); //实例化info
Producer pro=new Producer(info); //生产者
Consumer con=new Consumer(info); //消费者
new Thread(pro).start();
new Thread(con).start();
}
}
一个生产者与消费者的Demo,小弟有点迷糊,感觉有点明白,但是又不能很清楚,希望哪位大侠现身,解决下啊,小弟在线等啊,谢了 啊 !!!!
解决方案 »
- 庆祝下,csdn空间里面的分组一个很明显的bug终于解决了,这个bug至少存在一周多了。
- 我的jtable无法更新,应该是简单的问题,麻烦替我看下
- 关于getactioncommand的用法
- 【你们见过如此牛B的软件构架师吗?】
- MAP集合中键和值之间是如何建立映射关系的?
- sayhello.java编译成.html以后运行提示“未初始化小程序”,怎么回事?求教!
- socket 通信的问题,大家帮忙分析一下!
- 实现我的诺言,散分,主要回答人如下:wjmmml(笑着悲伤),xmvigour(微电--有狼的气质 没狼的勇气 真郁闷),sharetop(燕赤霞),piggybank(
- 高分求教:请问linux下怎么运行jdk
- JAVA程序的编译
- 谁能帮我看看这段程序,问题出在choice.select("")上;
- 获取网站源文时的乱码问题
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info=new Info(); //实例化info
Producer pro=new Producer(info); //生产者
Consumer con=new Consumer(info); //消费者
new Thread(pro).start();
new Thread(con).start();
}
先分析这个
Producer pro=new Producer(info); //生产者
new Thread(pro).start(); 执行它Producer里的run方法
public void run(){ //覆写run()方法
boolean flag=false;
for (int i=0;i <50 ;i++ ){
if (flag){
this.info.set("徐彦","程序员");
flag=false;
}
else{
this.info.set("希哲","教师");
flag=true;
}
}
}
第一次把this.info.set("希哲","教师");set 进去并且flag=true
第一轮flag = flase
public synchronized void set(String name,String content){
if (!flag){
try{
super.wait(); //线程停止
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.setName(name); //设置名称
try{
Thread.sleep(100); //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
this.setContent(content); //设置内容
flag=false;
super.notify(); //唤醒另一个线程消费者
}
Consumer con=new Consumer(info); //消费者
new Thread(con).start();
想一想也是开始拿出来打印了其实思路是很简单的。