黑马程序员技术交流社区
标题:
多线程,生产者消费者问题
[打印本页]
作者:
xibozglr
时间:
2013-11-29 08:44
标题:
多线程,生产者消费者问题
本帖最后由 xibozglr 于 2013-11-29 19:07 编辑
生产者消费者问题,为什么t1和t2线程执行不完啊???到这晕菜了,线程是怎么循环啊??什么时候线程执行完毕啊???
//描述资源
class ZiYuan
{
private String name;
private int count;
//定义标记
private boolean flag;
//提供给商品赋值的方法
public synchronized void set(String name)
{
if(flag)//判断标记为true,执行wait等待,为false,就生产
{
try
{
wait();
}
catch (InterruptedException e)
{
}
}
this.name=name+"--"+count;
count++;
System.out.println(Thread.currentThread().getName()+"生产"+this.name);
//生产完毕,将标记改为true
flag=true;
//唤醒消费者
notify();
}
//提供获取商品的方法
public synchronized void get()
{
if(!flag)
{
try
{
wait();
}
catch (InterruptedException e)
{
}
}
System.out.println(Thread.currentThread().getName()+"消费"+this.name);
//消费完毕,将标记改为false
flag=false;
//唤醒生产者
notify();
}
}
class ShengChanZhe implements Runnable
{
private ZiYuan z;
public ShengChanZhe(ZiYuan z)
{
this.z=z;
}
public void run()
{
z.set("面包");
}
}
class XiaoFeiZhe implements Runnable
{
private ZiYuan z;
public XiaoFeiZhe(ZiYuan z)
{
this.z=z;
}
public void run()
{
z.get();
}
}
class ShengChanXiaoFei
{
public static void main(String[] args)
{
//创建资源
ZiYuan z=new ZiYuan();
//创建两个任务
ShengChanZhe s=new ShengChanZhe(z);
XiaoFeiZhe x=new XiaoFeiZhe(z);
//创建线程
Thread t1=new Thread(s);
Thread t2=new Thread(x);
t1.start();
t2.start();
}
}
作者:
刘敏
时间:
2013-11-29 09:39
public void run()
{
while(true)
{
z.get();
}
}
把两个重写的run()方法里面加上 while 循环,否则只执行一次线程就结束了
作者:
xibozglr
时间:
2013-11-29 09:46
是这个程序,多生产多消费,为什么线程一直循环执行啊?
//描述资源
class ZiYuan
{
private String name;
private int count;
//定义标记
private boolean flag;
//提供给商品赋值的方法
public synchronized void set(String name)
{
while(flag)//判断标记为true,执行wait等待,为false,就生产
{
try
{
wait();
}
catch (InterruptedException e)
{
}
}
this.name=name+"--"+count;
count++;
System.out.println(Thread.currentThread().getName()+"生产"+this.name);
//生产完毕,将标记改为true
flag=true;
//唤醒所有等待线程
notifyAll();
}
//提供获取商品的方法
public synchronized void get()
{
while(!flag)
{
try
{
wait();
}
catch (InterruptedException e)
{
}
}
System.out.println(Thread.currentThread().getName()+"消费"+this.name);
//消费完毕,将标记改为false
flag=false;
//唤醒所有等待线程
notifyAll();
}
}
class ShengChanZhe implements Runnable
{
private ZiYuan z;
public ShengChanZhe(ZiYuan z)
{
this.z=z;
}
public void run()
{
z.set("面包");
}
}
class XiaoFeiZhe implements Runnable
{
private ZiYuan z;
public XiaoFeiZhe(ZiYuan z)
{
this.z=z;
}
public void run()
{
z.get();
}
}
class ShengChanXiaoFei2
{
public static void main(String[] args)
{
//创建资源
ZiYuan z=new ZiYuan();
//创建两个任务
ShengChanZhe s=new ShengChanZhe(z);
XiaoFeiZhe x=new XiaoFeiZhe(z);
//创建线程
Thread t0=new Thread(s);
Thread t1=new Thread(s);
Thread t2=new Thread(x);
Thread t3=new Thread(x);
t0.start();
t1.start();
t2.start();
t3.start();
}
}
作者:
刘敏
时间:
2013-11-29 09:55
你这个线程也没有循环效果啊
作者:
刘敏
时间:
2013-11-29 09:56
xibozglr 发表于 2013-11-29 09:46
是这个程序,多生产多消费,为什么线程一直循环执行啊?
//描述资源
Thread-0生产面包--0
Thread-2消费面包--0
Thread-1生产面包--1
Thread-3消费面包--1
就只有这4句话啊
作者:
刘敏
时间:
2013-11-29 09:58
xibozglr 发表于 2013-11-29 09:46
是这个程序,多生产多消费,为什么线程一直循环执行啊?
//描述资源
你看看是不是你java XX 的时候,执行的文件写错了,或者javac的时候写错了
作者:
李文帅
时间:
2013-11-29 10:12
楼主的程序两个线程 都执行完毕了啊
线程执行完毕,实际上就是线程中run()方法中的代码执行完毕,
对于线程如何循环:线程开启后,执行的是run()方法中的代码,所以线程如何循环就是看run()方法中的循环
对于楼主的程序,生产者和消费者的run()方法中都只有一句代码,并没有循环,所以当其各自的run()方法中
的代码执行完毕之后,两个线程也就执行完毕了
作者:
xibozglr
时间:
2013-11-29 12:05
懂了,while
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2