黑马程序员技术交流社区
标题:
生产者,消费者,问题,搞了两个小时不知道哪里错了,求助
[打印本页]
作者:
碎流
时间:
2014-9-15 09:32
标题:
生产者,消费者,问题,搞了两个小时不知道哪里错了,求助
本帖最后由 碎流 于 2014-9-15 19:00 编辑
package cn.fuxi12;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//新特性生产者消费者
class ziYuanb
{
private String name;
private int jiShu = 0;
private boolean bl = false;
private Lock lock = new ReentrantLock();
private Condition sheng = lock.newCondition();
private Condition xiao = lock.newCondition();
public void shengc(String name)throws InterruptedException
{
lock.lock();
try
{
while(bl)
sheng.await(); //1 2
this.name = name+".."+jiShu++;
System.out.println(Thread.currentThread().getName()+",,,,,,,生产者"+this.name+jiShu);
bl = true;
sheng.signal();
}
finally
{
lock.unlock();
}
}
public void xiaoc() throws InterruptedException
{
lock.lock();
try
{
while(!bl)
xiao.await(); //3
System.out.println(Thread.currentThread().getName()+".....消费者"+this.name+jiShu);
bl = false;
xiao.signal();
}
finally
{
lock.unlock();
}
}
}
class shengChanZhe implements Runnable
{
private ziYuanb zy;
shengChanZhe(ziYuanb zy)
{
this.zy = zy;
}
public void run()
{
while(true)
{
try
{
zy.shengc("+煎饼果子");
} catch (InterruptedException e)
{
}
}
}
}
class xiaoFeiZhe implements Runnable
{
private ziYuanb zy;
xiaoFeiZhe(ziYuanb zy)
{
this.zy = zy;
}
public void run()
{
while(true)
{
try {
zy.xiaoc();
} catch (InterruptedException e) {
}
}
}
}
public class Demo06 {
public static void main(String[] args) {
ziYuanb zy = new ziYuanb();
shengChanZhe sh = new shengChanZhe(zy);
xiaoFeiZhe xf = new xiaoFeiZhe(zy);
Thread t1 = new Thread(sh);
Thread t2 = new Thread(sh);
Thread t3 = new Thread(xf);
Thread t4 = new Thread(xf);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
本来是自己写的,眼看改的都和毕老师一样了,还是不知道哪里出问题,,,,,输出都会卡住,,,像是死锁,,,,想了两个多小时,实在不知道怎么搞,求指点.....或许可能就是很小的问题,但是就是解决不了..
作者:
李春丽
时间:
2014-9-15 11:11
本帖最后由 李春丽 于 2014-9-15 11:20 编辑
package cn.fuxi12;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//新特性生产者消费者
class ziYuanb
{
private String name;
private int jiShu = 0;
private boolean bl = false;
private Lock lock = new ReentrantLock();
private Condition sheng = lock.newCondition();
private Condition xiao = lock.newCondition();
public void shengc(String name)throws InterruptedException
{
lock.lock();
try
{
while(bl)
sheng.await(); //1 2
this.name = name+".."+jiShu++;
System.out.println(Thread.currentThread().getName()+",,,,,,,生产者"+this.name+jiShu);
bl = true;
xiao.signal();
}
finally
{
lock.unlock();
}
}
public void xiaoc() throws InterruptedException
{
lock.lock();
try
{
while(!bl)
xiao.await(); //3
System.out.println(Thread.currentThread().getName()+".....消费者"+this.name+jiShu);
bl = false;
sheng.signal();
}
finally
{
lock.unlock();
}
}
}
class shengChanZhe implements Runnable
{
private ziYuanb zy;
shengChanZhe(ziYuanb zy)
{
this.zy = zy;
}
public void run()
{
while(true)
{
try
{
zy.shengc("+煎饼果子");
} catch (InterruptedException e)
{
}
}
}
}
class xiaoFeiZhe implements Runnable
{
private ziYuanb zy;
xiaoFeiZhe(ziYuanb zy)
{
this.zy = zy;
}
public void run()
{
while(true)
{
try {
zy.xiaoc();
} catch (InterruptedException e) {
}
}
}
}
public class Demo06 {
public static void main(String[] args) {
ziYuanb zy = new ziYuanb();
shengChanZhe sh = new shengChanZhe(zy);
xiaoFeiZhe xf = new xiaoFeiZhe(zy);
Thread t1 = new Thread(sh);
Thread t2 = new Thread(sh);
Thread t3 = new Thread(xf);
Thread t4 = new Thread(xf);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
复制代码
try {
while(bl)
sheng
.await(); //1 2
this.name = name+".."+jiShu++;
System.out.println(Thread.currentThread().getName()+",,,,,,,生产者"+this.name+jiShu);
bl = true;
xiao
.signal();//sheng.signal();
注意,之前是消费者等待,然后,标记设为 false ,就应该让生产者唤醒啦
}
建立多个 Condition 对象,使用时要注意分析:谁等待着,需要谁唤醒了
作者:
勇闯黑马
时间:
2014-9-27 10:39
import java.util.concurrent.locks.*;
//新特性生产者消费者
class ziYuanb
{
private String name;
private int jiShu = 0;
private boolean bl = false;
private Lock lock = new ReentrantLock();
private Condition sheng = lock.newCondition();
private Condition xiao = lock.newCondition();
public void shengc(String name)throws InterruptedException
{
lock.lock();
try
{
while(bl)
sheng.await(); //1 2
this.name = name+".."+jiShu++;
System.out.println(Thread.currentThread().getName()+",,,,,,,生产者"+this.name+jiShu);
bl = true;
xiao.signalAll();
}
finally
{
lock.unlock();
}
}
public void xiaoc() throws InterruptedException
{
lock.lock();
try
{
while(!bl)
xiao.await(); //3
System.out.println(Thread.currentThread().getName()+".....消费者"+this.name+jiShu);
bl = false;
sheng.signalAll();
}
finally
{
lock.unlock();
}
}
}
class shengChanZhe implements Runnable
{
private ziYuanb zy=new ziYuanb();
shengChanZhe(ziYuanb zy)
{
this.zy = zy;
}
public void run()
{
while(true)
{
try
{
zy.shengc("+煎饼果子");
} catch (InterruptedException e)
{
}
}
}
}
class xiaoFeiZhe implements Runnable
{
private ziYuanb zy=new ziYuanb();;
xiaoFeiZhe(ziYuanb zy)
{
this.zy = zy;
}
public void run()
{
while(true)
{
try {
zy.xiaoc();
} catch (InterruptedException e) {
}
}
}
}
class Demo06 {
public static void main(String[] args) {
ziYuanb zy = new ziYuanb();
shengChanZhe sh = new shengChanZhe(zy);
xiaoFeiZhe xf = new xiaoFeiZhe(zy);
Thread t1 = new Thread(sh);
Thread t2 = new Thread(sh);
Thread t3 = new Thread(xf);
Thread t4 = new Thread(xf);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2