有个地方不解,请各位指点一下!!![code=java]
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerConsumerDemo2 {
public static void main(String[] args) {
Resource2 r = new Resource2();
Producer2 pro = new Producer2(r);
Consumer2 con = new Consumer2(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
//资源
class Resource2 {
private String name;
private int count = 1;
private boolean flag = true;
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name) {
lock.lock();
/*============问题在这,请留意while语句的位置===========*/
while (flag) {
try {
condition_pro.await();
this.name = name + "......" + count++;
System.out.println(Thread.currentThread().getName() + "....生产者...." + this.name);
flag = true;
condition_con.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public void out() {
lock.lock();
/*============问题在这,请留意while语句的位置===========*/
while (!flag) {
try {
condition_con.await();
System.out.println(Thread.currentThread().getName() + "....消费者........." + this.name);
flag = false;
condition_pro.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}
//生产者
class Producer2 implements Runnable {
private Resource2 res;
public Producer2(Resource2 res) {
this.res = res;
}
@Override
public void run() {
while (true) {
res.set("++商品++");
}
}
}
//消费者
class Consumer2 implements Runnable {
private Resource2 res;
public Consumer2(Resource2 res) {
this.res = res;
}
@Override
public void run() {
while (true) {
res.out();
}
}
}[/code]以上程序什么都没有输出,改为下面的代码后就有输出了,代码如下[code=java]
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerConsumerDemo2 {
public static void main(String[] args) {
Resource2 r = new Resource2();
Producer2 pro = new Producer2(r);
Consumer2 con = new Consumer2(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
//资源
class Resource2 {
private String name;
private int count = 1;
private boolean flag = true;
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name) {
lock.lock();
/*============问题在这,请留意while语句的位置===========*/
{
try {
while (flag)
condition_pro.await();
this.name = name + "......" + count++;
System.out.println(Thread.currentThread().getName() + "....生产者...." + this.name);
flag = true;
condition_con.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public void out() {
lock.lock();
/*============问题在这,请留意while语句的位置===========*/
{
try {
while (!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName() + "....消费者........." + this.name);
flag = false;
condition_pro.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}
//生产者
class Producer2 implements Runnable {
private Resource2 res;
public Producer2(Resource2 res) {
this.res = res;
}
@Override
public void run() {
while (true) {
res.set("++商品++");
}
}
}
//消费者
class Consumer2 implements Runnable {
private Resource2 res;
public Consumer2(Resource2 res) {
this.res = res;
}
@Override
public void run() {
while (true) {
res.out();
}
}
}[/code]
[ 本帖最后由 黄敏文 于 2011-08-24 17:11 编辑 ] |