生产者和消费者关系通过多线程来实现




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();
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值