黑马程序员技术交流社区
标题:
多线程,消费者问题。。。
[打印本页]
作者:
november
时间:
2014-1-16 23:35
标题:
多线程,消费者问题。。。
生产者消费者问题,为什么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();
}
}
作者:
§傻、才乖
时间:
2014-1-16 23:47
这个问题说起来有点复杂
建议多看几遍毕老师关于线程的讲解,很详细,如果跟着毕老师的思路听下云,一定能理解
剩下的就是自己模拟了
作者:
月生春
时间:
2014-1-16 23:49
本帖最后由 月生春 于 2014-1-16 23:50 编辑
你在资源中没有设定资源的多少啊,那样肯定会一直运行下去,比如毕老师买票的例子,卖一百张票,卖掉一张就少一张,最后全部卖完!
我将代码改动一下,定义 count为100;
然后资源被占用时count就减减
class ZiYuan
{
private String name;
private int count=100;
//定义标记
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 MyTest3
{
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();
}
}
作者:
闺蜜配男友‘好
时间:
2014-1-17 02:08
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
作者:
panzhenglian
时间:
2014-1-17 10:33
你的两个线程 (不包括主线程) 已近执行完了,但是每个线程只执行了一次,因为线程没有循环结构,代码执行完,程序也就结束了,
作者:
panzhenglian
时间:
2014-1-17 10:38
对楼主的代码稍加改动,加入了while循环,则每个线程执行100次结束,代码如下
//package day21;
class ZiYuan
{
private String name;
private int count=100;
//定义标记
private boolean flag;
//提供给商品赋值的方法
public synchronized void set(String name)
{
while(count>0){
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()
{
while(count>0){
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 MyTest3
{
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();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2