线程的通信-生产者消费者

当对于一个生产者和一个消费者时

public static void main(String[] agrs) {
    Resource res = new Resource();
    Producer p = new Producer(res);
    Consumer c = new Consumer(res);
    
    Thread t1 = new Thread(p);
    Thread t2 = new Thread(c);
    t1.start();
    t2.start();
}
class Resource {
    private String name;
    private int cont = 1;
    private boolean flag = false;
    public synchronized void set(String name) {
        if(flag) {
            try{
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.name = name + "--" + cont++;
        System.out.println(Thread.currentThread().getName()+"- producer -"+this.name);
        flag = true;
        this.notify();
    }
    public synchronized void out() {
        if(!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"---- consumer ----"+this.name);
        flag = false;
        this.notify();
    }
}
class Producer implements Runnable {
    private Resource res;
    Producer(Resource res) {
        this.res = res;
    }
    public void run() {
        while(true) {
            res.set("wares");
        }
    }
}
class Consumer implements Runnable {
    private Resource res;
    Consumer(Resource res) {
        this.res = res;
    }
    public void run() {
        while(true) {
            res.out();
        }
    }
} 

当对于多个生产者和消费者时

public static void main(String[] agrs) {
        Resource res = new Resource();
        Producer p = new Producer(res);
        Consumer c = new Consumer(res);
        
        Thread t1 = new Thread(p);
        Thread t2 = new Thread(p);
        Thread t3 = new Thread(c);
        Thread t4 = new Thread(c);
        
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

会出现生产一个商品,消费出两个商品,或者生产两个而消费一个
892430-20160506200452951-203822170.png

这是因为if语句只判断一次flag标记,消费者的线程生产完会唤醒本方的另一条生产线程,而不判断flag标记,导致生产两次而消费一次

然后我们可以把if换成while
原因:让被唤醒的线程再一次的判断

while(flag) {
    try{
        this.wait();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

但是,新的问题又产生了,线程会全部停止
原因:线程唤醒往往是唤醒线程池的第一个,所以会唤醒本方的线程,会导致数据错乱,加入while后,会导致全部的线程都在等待。我们应该唤醒的是消费者的线程,所以我们可以用notifyAll,唤醒所有的线程,因为会while会判断flag标记,所以不会唤醒生产者的线程,而是唤醒消费者的线程
notify,容易出现只唤醒本方线程的情况,导致程序中的所有线程等待

while(flag) {
    try{
        this.wait();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
this.name = name + "--" + cont++;
System.out.println(Thread.currentThread().getName()+"- producer -"+this.name);
flag = true;
this.notifyALL();

最后改进

public class Dealer {
    public static void main(String[] agrs) {
        Resource res = new Resource();
        Producer p = new Producer(res);
        Consumer c = new Consumer(res);
        
        Thread t1 = new Thread(p);
        Thread t2 = new Thread(p);
        Thread t3 = new Thread(c);
        Thread t4 = new Thread(c);
        
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class Resource {
    private String name;
    private int cont = 1;
    private boolean flag = false;
    public synchronized void set(String name) {
        while(flag) {
            try{
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.name = name + "--" + cont++;
        System.out.println(Thread.currentThread().getName()+"- producer -"+this.name);
        flag = true;
        this.notifyALL();
    }
    public synchronized void out() {
        while(!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"---- consumer ----"+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("wares");
        }
    }
}
class Consumer implements Runnable {
    private Resource res;
    Consumer(Resource res) {
        this.res = res;
    }
    public void run() {
        while(true) {
            res.out();
        }
    }
} 

转载于:https://www.cnblogs.com/rancvl/p/5466465.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值