黑马程序员技术交流社区
标题:
生产者消费者问题加锁实现
[打印本页]
作者:
耿渊博
时间:
2014-3-25 22:31
标题:
生产者消费者问题加锁实现
本帖最后由 耿渊博 于 2014-3-25 23:17 编辑
下面代码可以实现带锁的生产者消费者问题吗?
package com.Thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerConsumerDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource r = new Resource();
Producer pro = new Producer(r);
Consume con = new Consume(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 Resource{
private String name;
private int count = 1;
private boolean flag = false;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void set(String name)throws InterruptedException{
lock.lock();
try{
while(flag)
condition.await();
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag = true;
condition.signalAll();
}
finally{
lock.unlock();
}
}
public void out()throws InterruptedException{
lock.lock();
try{
while(!flag)
condition.await();
System.out.println(Thread.currentThread().getName()+"...消费者.............."+this.name);
flag = false;
condition.signalAll();
}finally{
lock.unlock();
}
}
}
class Producer implements Runnable{
private Resource res;
Producer(Resource res){
this.res = res;
}
public void run(){
while(true){
try {
res.set("+商品+");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consume implements Runnable{
private Resource res;
Consume(Resource res){
this.res = res;
}
public void run(){
while(true)
try {
res.out();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
复制代码
作者:
papercup
时间:
2014-3-25 22:46
你写的这个不太好,唤醒进程时需要分开唤醒。即定义两个condition 变量。我给你改了一下
package com.Thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerConsumerDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource r = new Resource();
Producer pro = new Producer(r);
Consume con = new Consume(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 Resource{
private String name;
private int count = 1;
private boolean flag = false;
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();//消费者
private Condition condition_con = lock.newCondition();//生产者
public void set(String name)throws InterruptedException{
lock.lock();
try{
while(flag)
condition_pro.await();//生产者等待
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag = true;
condition_con.signal();//唤醒消费者
}
finally{
lock.unlock();
}
}
public void out()throws InterruptedException{
lock.lock();
try{
while(!flag)
condition_con.await();//消费者等待
System.out.println(Thread.currentThread().getName()+"...消费者.............."+this.name);
flag = false;
condition_pro.signal();//唤醒生产者
}finally{
lock.unlock();
}
}
}
class Producer implements Runnable{
private Resource res;
Producer(Resource res){
this.res = res;
}
public void run(){
while(true){
try {
res.set("+商品+");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consume implements Runnable{
private Resource res;
Consume(Resource res){
this.res = res;
}
public void run(){
while(true)
try {
res.out();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
复制代码
作者:
耿渊博
时间:
2014-3-25 23:16
papercup 发表于 2014-3-25 22:46
你写的这个不太好,唤醒进程时需要分开唤醒。即定义两个condition 变量。我给你改了一下 ...
明白了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2