多线程之生产者和消费者模式

多线程生产者只有多个生产者,多个消费者!这里不讲基础的知识。

代码如下

package Thread;
class Resource {
    private String name;
    private int count=0;
    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;
        this.notifyAll();//这里也要用notifyAll()否则,可能造成所有的线程都在等待。
    }
    public synchronized void out(){
        while(!flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
        flag=false;
        this.notifyAll();
    }
}
class Producer implements Runnable{
    private  Resource res;
    Producer(Resource res){
        this.res=res;
    }

    public void run() {
        while(true){
            res.set("商品");
        }
    }
}
class Consumer implements Runnable{
    private Resource res;
    Consumer(Resource res){
        this.res=res;
    }
    public void run() {
        while(true){
            res.out();
        }
    }
}
public class ProducterConsumerDemo {
    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(pro);
        Thread t3=new Thread(con);
        Thread t4=new Thread(con);

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }
}

ab.png

这是运行的结果。可以看出都是一个生产一个消费。


然而当jdk1.5之后,我们可以用一些新特性去解决这个问题。java.util.concurrent.locks包下面的Lock接口,Condition接口。

这里其实Lock可以代替synchronized,而Condition所替代监视器。

这里而且Lock可以产生多个Condition监视器。

class Resource{
    private int count=0;
    private String name;
    private boolean flag=false;
    Lock lock=new ReentrantLock();
    Condition pro_condition=lock.newCondition();//这里是生产者的监视器
    Condition con_condition=lock.newCondition();//这里是消费者的监视器
    public void set(String name){

        while(true) {
            try {
                lock.lock();//这里相当于代替了synchroinzed 代码块。这不过更人性,直接说上锁。
                while (flag) {
                    try {
                        pro_condition.await();//但是应为await 会抛出异常但不是放锁,所以就必须在finally里释放锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                this.name = name + "--" + count++;
                System.out.println(Thread.currentThread().getName() + "-生产者.." + this.name);
                flag = true;//生产完成后将标志置为真!
                con_condition.signal(); //等价于this.notifyAll这里锁匙this对象
            } catch (Exception e) {
            } finally {
                lock.unlock();//但是应为await 会抛出异常但不是放锁,所以就必须在finally里释放锁
            }

        }
    }

    public void out(){
        while(true) {
            lock.lock();
            try {
                while (flag == false) {
                    con_condition.wait();//即使有消费者线程被唤醒也必须判断flag的标志
                }
                System.out.println(Thread.currentThread().getName() + "-消费者.." + name);
                flag = false;
                pro_condition.signal();
            } catch (Exception e) {

            } finally {
                lock.unlock();
            }
        }
    }

}
class Producer implements Runnable{
    private Resource resource;
    Producer(Resource resource){
        this.resource=resource;
    }

    public void run() {
        resource.set("商品");
    }
}
class Consumer implements Runnable{
    private Resource resource;
    Consumer(Resource resource){
        this.resource=resource;
    }

    public void run() {
        resource.out();
    }
}
public class ProducterConsumerDemo{
    public static void main(String[] args) {
        Resource resource=new Resource();
        Producer producer=new Producer(resource);
        Consumer consumer=new Consumer(resource);
        Thread p1=new Thread(producer);
        Thread p2=new Thread(producer);
        Thread c1=new Thread(consumer);
        Thread c2=new Thread(consumer);
        p1.start();
        p2.start();
        c1.start();
        c2.start();
    }
}

这里可以看出,两个Condition对象,可以不用notifyAll()。生产者的锁需要消费者去打开,消费者可以用生产者打开。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章