import java.util.concurrent.locks.*;
/*import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;*/
/**
* 使用notify只能唤醒本方的线程 ,
* 对于多个生产者和消费者问题 为什么要定义while判断标记。
* 原因让唤醒的线程再一次判断标记。
* 为什么定义notifyall?因为需要唤醒对方线程,因为只有notify,
* 容易出现只唤醒本方线程的情况 导致程序中的所有线程都等待。
*
*
*jdk1.5中提供了多线程升级解决方案。将同步的synchronized替换成现实lock操作
*将object中的wait,notify,notifyall替换成condition对象
*该对象可以lock锁,进行获取
*该事例中,实现了本方只唤醒对方操作。
*/
/*有问题,不出现结果原因是while括号的问题把输出和输入语句包含在内了,这样作是不对的*/
public class ProduceerConsumerDemo {
public static void main(String[] args) {
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(con);
Thread t3 = new Thread(pro);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}/*
* //生产者线程 //使用if就会数据错乱,如果使用notify就会产生全部等待 class Producer implements
* Runnable{
*
* private Resource res; public Producer(Resource res) { super(); this.res =
* res; }
*
* @Override public void run() { while (true) { res.set("+商品+"); }
*
*
* }
*
* } //消费者线程 class Consumer implements Runnable{ private Resource res;
*
* public Consumer(Resource res) { super(); this.res = res; }
*
* @Override public void run() { while (true) { res.out(); } }
*
* }
*
*
*
*
* //生产者 class Resource{ private String name; private int count=1; private
* boolean flag=false; public synchronized void set (String name) { while
* (flag) try { wait(); } catch (InterruptedException e) {
* e.printStackTrace(); } this.name=name+"..."+count++;//给产品编号
* System.out.println(Thread.currentThread().getName()+"生产者:"+this.name);
* flag=true; notifyAll(); } public synchronized void out() { while(!flag)
* try { wait(); } catch (InterruptedException e) { e.printStackTrace(); }
* System.out.println(Thread.currentThread().getName()+"消费者:"+this.name);
* flag=false; notifyAll();
*
* } }
*/
// 使用lock进行同步
// 生产者
class Resource {
private String name;
private int count = 1;
private boolean flag = false;
private Lock lock = new ReentrantLock();// 创建锁
private Condition conditionpro = lock.newCondition();// 拿到锁
private Condition conditioncon = lock.newCondition();
public void set(String name) {
lock.lock();// 同步
try {
while (flag)
conditionpro.await();// 等待
this.name = name + "..." + count++;// 给产品编号
System.out.println(Thread.currentThread().getName() + "生产者:" + this.name);
flag = true;
conditioncon.signal();// 唤醒某一个消费者
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();// 解锁
}
}
public void out() {
lock.lock();
try {
while (!flag)
conditioncon.await();
System.out.println(Thread.currentThread().getName() + "消费者:" + this.name);
flag = false;
conditionpro.signal();// 唤醒生产者
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.unlock();//释放锁的动作一定要执行
}
}
// 生产者线程
class Producer implements Runnable {
private Resource res;
public Producer(Resource res) {
this.res = res;
}
@Override
public void run() {
while (true) {
res.set("+商品+");
}
}
}
// 消费者线程
class Consumer implements Runnable {
private Resource res;
public Consumer(Resource res) {
this.res = res;
}
@Override
public void run() {
while (true) {
res.out();
}
}
}