打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java 多线程生产者消费者 Lock对象

 

class Res {    private String name;    private int count = 1;    private boolean flag;    public synchronized void set(String name) {        while (flag) {            try {                this.wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        this.name = name + "--" + count++;        System.out.println(Thread.currentThread().getName() + "...生产者..." + this.name);        flag = true;        this.notifyAll();    }    public synchronized void print() {        while (flag) {            System.out.println(Thread.currentThread().getName() + "......消费者......" + this.name);            flag = false;            this.notifyAll();        }    }}class Producer implements Runnable {    private Res r;    public Producer(Res r) {        this.r = r;    }    @Override    public void run() {        while (true) {            r.set("商品");        }    }}class Consumer implements Runnable {    private Res r;    public Consumer(Res r) {        this.r = r;    }    @Override    public void run() {        while (true) {            r.print();        }    }}public class ProducerConsumerDemo {    public static void main(String[] args) {        Res r = new Res();        new Thread(new Producer(r)).start();        new Thread(new Producer(r)).start();        new Thread(new Consumer(r)).start();        new Thread(new Consumer(r)).start();    }}
出现多个生产者消费者要用while重新判断一次标记,并使用notifyAll()唤醒所有,notify可能出现只唤醒本方线程的情况,导致所有线程都等待。
缺点:每次都唤醒所有,其实只需要生产者唤醒消费者,消费者唤醒生产者就行了,JDK1.5中就实现了。
import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;class Res {    private String name;    private int count = 1;    private boolean flag;    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 print() 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 Res r;    public Producer(Res r) {        this.r = r;    }    @Override    public void run() {        while (true) {            try {                r.set("商品");            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}class Consumer implements Runnable {    private Res r;    public Consumer(Res r) {        this.r = r;    }    @Override    public void run() {        while (true) {            try {                r.print();            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}public class ProducerConsumerDemo {    public static void main(String[] args) {        Res r = new Res();        new Thread(new Producer(r)).start();        new Thread(new Producer(r)).start();        new Thread(new Consumer(r)).start();        new Thread(new Consumer(r)).start();    }}

将synchronized替换成显示的Lock操作,将Object中的wait,notify,notifyAll替换成了Condition对象,该对象可以通过Lock锁获取。

实现了本方只唤醒对方的操作。



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
多线程案例
深入java并发Lock一
java中的多线程的实现生产者消费者模式
Java多线程之并发协作生产者消费者设计模式
Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服