class Info
{
private boolean flag=false; //定义标志位
private String name; //定义姓名字段
private String content; //定义内容字段
public String getName() //获取姓名信息
{
return this.name;
}
public String getContent()//获取内容信息
{
return this.content;
}
public void setName(String name) //设置姓名
{
this.name=name;
}
public void setContent(String content) //设置内容字段
{
this.content=content;
}
public synchronized void set(String name,String content)
{
if(!flag)
{
super.notify(); //唤醒线程,进行排队
this.setName(name); //设置姓名字段
try
{
Thread.sleep(300);
}
catch (Exception e)
{
e.printStackTrace(); //输出异常
}
this.setContent(content);//设置内容字段
flag=true;
}
else
{
try
{
super.wait(); //暂停线程
}
catch (Exception e)
{
e.printStackTrace(); //输出异常
}
flag=false;
}
}
public synchronized void get()
{
if(flag)
{
super.notify(); //唤醒线程
try
{
Thread.sleep(200);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"消费者:产品信息--->名称:"+this.getName()+" 内容:"+this.getContent());
flag=false;
}
else
{
try
{
super.wait(); //线程等待
}
catch (Exception e)
{
e.printStackTrace();
}
flag=true;
}
}
}
class Consumer implements Runnable //实现Runnable接口
{
private Info info=null; //消费者消费产品
Consumer(Info info) //构造函数
{
this.info=info; //对象直接"="操作
}
public void run() //重写run()方法
{
for(int i=0;i<50;i++)
{
this.info.get();
}
}
}
class Producter implements Runnable //实现Runnable接口
{
private Info info=null; //生产者生产产品
Producter(Info info) //构造方法
{
this.info=info;
}
public void run() //重写run()方法
{
boolean flag=false; //定义标志位
for(int i=0;i<50;i++)
{
if(flag)
{
this.info.set("李兴华","JAVA讲师");
flag=false;
}
else
{
this.info.set("王国亮","千万富翁");
flag=true;
}
}
}
}
public class ThreadCaseDemo02
{
public static void main(String args[])
{
Info i=new Info(); //生成产品
Consumer a=new Consumer(i); //消费者对象
Producter b=new Producter(i); //生产者对象
Thread c=new Thread(a);
Thread d=new Thread(b);
d.start();
c.start();
}
}
求解呀。我想问:为什么同步机制已经加载进入函数里边,然后用了Object类的唤醒等待机制,还是无法实现生产一个、消费一个产品的目的???。各位大侠帮忙解释一下呀!!!
{
private boolean flag=false; //定义标志位
private String name; //定义姓名字段
private String content; //定义内容字段
public String getName() //获取姓名信息
{
return this.name;
}
public String getContent()//获取内容信息
{
return this.content;
}
public void setName(String name) //设置姓名
{
this.name=name;
}
public void setContent(String content) //设置内容字段
{
this.content=content;
}
public synchronized void set(String name,String content)
{
if(!flag)
{
super.notify(); //唤醒线程,进行排队
this.setName(name); //设置姓名字段
try
{
Thread.sleep(300);
}
catch (Exception e)
{
e.printStackTrace(); //输出异常
}
this.setContent(content);//设置内容字段
flag=true;
}
else
{
try
{
super.wait(); //暂停线程
}
catch (Exception e)
{
e.printStackTrace(); //输出异常
}
flag=false;
}
}
public synchronized void get()
{
if(flag)
{
super.notify(); //唤醒线程
try
{
Thread.sleep(200);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"消费者:产品信息--->名称:"+this.getName()+" 内容:"+this.getContent());
flag=false;
}
else
{
try
{
super.wait(); //线程等待
}
catch (Exception e)
{
e.printStackTrace();
}
flag=true;
}
}
}
class Consumer implements Runnable //实现Runnable接口
{
private Info info=null; //消费者消费产品
Consumer(Info info) //构造函数
{
this.info=info; //对象直接"="操作
}
public void run() //重写run()方法
{
for(int i=0;i<50;i++)
{
this.info.get();
}
}
}
class Producter implements Runnable //实现Runnable接口
{
private Info info=null; //生产者生产产品
Producter(Info info) //构造方法
{
this.info=info;
}
public void run() //重写run()方法
{
boolean flag=false; //定义标志位
for(int i=0;i<50;i++)
{
if(flag)
{
this.info.set("李兴华","JAVA讲师");
flag=false;
}
else
{
this.info.set("王国亮","千万富翁");
flag=true;
}
}
}
}
public class ThreadCaseDemo02
{
public static void main(String args[])
{
Info i=new Info(); //生成产品
Consumer a=new Consumer(i); //消费者对象
Producter b=new Producter(i); //生产者对象
Thread c=new Thread(a);
Thread d=new Thread(b);
d.start();
c.start();
}
}
求解呀。我想问:为什么同步机制已经加载进入函数里边,然后用了Object类的唤醒等待机制,还是无法实现生产一个、消费一个产品的目的???。各位大侠帮忙解释一下呀!!!
解决方案 »
- 关于Swing技术的那点事
- 大家都用什么开发工具?
- java.sql.SQLException: The SQL statement must not be null or empty.
- 为何会报错result = new sun.misc.BASE64Encoder().encode(rsaCipherText);
- org.apache.commons.beanutils.BeanUtils的copyProperties方法怎么copy有问题啊
- Jpanel怎样动态删除他上面的控件。
- java大数值计算问题!
- 关于bean:write,怎么格式化输出?
- 欲贱卖到北京(一java程序员)!
- 怎樣彈出仰慕像vc里的一樣的MessageBox?
- 一个关于java的小问题请教各位大师
- 关于JTextArea和TextArea的问题
if(!flag){
super.notify(); //唤醒线程,进行排队
this.setName(name); //设置姓名字段
try
{
Thread.sleep(300);
}
catch (Exception e)
{
e.printStackTrace(); //输出异常
}
this.setContent(content);//设置内容字段
flag=true;
}
else
{
try
{
super.wait(); //暂停线程
}
catch (Exception e)
{
e.printStackTrace(); //输出异常
}
flag=false;
}
加了。。标志位的。
while (condition == false)
wait do your work
notify all}
if(!flag)
{
this.setName(name); //设置姓名字段
try
{
Thread.sleep(300);
}
catch (Exception e)
{
e.printStackTrace(); //输出异常
}
this.setContent(content);//设置内容字段
super.notify(); //唤醒线程,进行排队
flag=true;
}
try
{
super.wait(); //暂停线程
}
catch (Exception e)
{
e.printStackTrace(); //输出异常
}
flag=false;
}
我把以上代码的set部分修改之后如上想问各位高手是不是其中一个线程休眠之后。。另一个线程就会run()起来。。,假设produter()线程休眠,是不是consumer就会run()起来。