Java 多线程 (PART XVIII) Lock(II)用lock实现生产者消费者模式

来源:互联网 发布:10月外贸数据 编辑:程序博客网 时间:2024/05/18 02:03

Object和Condition

Object类中的方法有很多可以使用Condition中的方法来代替。

Object Condition wait() await() wait(long timeout) await(long time,TimeUnit unit) notify() signal() notifyAll() signalAll()

由这张表可以得知,Condition类可以实现线程通信。

Lock &&Condition 实现生产者/消费者模式

示例代码:

package hello;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;class ObjectValue {    public static String Value = "";}class Service {    private Lock lock = new ReentrantLock();    private Condition condition = lock.newCondition();    public void setValue() {        try {            lock.lock();            while (!ObjectValue.Value.equals("")) {                condition.await();            }            String value = System.currentTimeMillis() + "_" + System.nanoTime();            System.out.println("set的值是:" + value);            ObjectValue.Value = value;            condition.signal();        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            lock.unlock();        }    }    public void getValue() {        try {            lock.lock();            while (ObjectValue.Value.equals("")) {                condition.await();            }            System.out.println("get 的值是:" + ObjectValue.Value);            ObjectValue.Value = "";            condition.signal();        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            lock.unlock();        }    }}class ThreadP extends Thread {    private Service service;    public ThreadP(Service service) {        this.service = service;    }    @Override    public void run() {        while (true) {            this.service.setValue();        }    }}class ThreadC extends Thread {    private Service service;    public ThreadC(Service service) {        this.service = service;    }    @Override    public void run() {        while (true) {            this.service.getValue();        }    }}public class Main {    public static void main(String[] args) {        Service service = new Service();        ThreadP threadp = new ThreadP(service);        threadp.start();        ThreadC threadc = new ThreadC(service);        threadc.start();    }}

和synchronized(Object lock)差不多:

运行结果:

部分

set的值是:1507639004441_32000465052113get 的值是:1507639004441_32000465052113set的值是:1507639004441_32000465066481get 的值是:1507639004441_32000465066481set的值是:1507639004441_32000465078797get 的值是:1507639004441_32000465078797set的值是:1507639004441_32000465090702get 的值是:1507639004441_32000465090702set的值是:1507639004441_32000465103018get 的值是:1507639004441_32000465103018set的值是:1507639004441_32000465114923get 的值是:1507639004441_32000465114923set的值是:1507639004441_32000465129702get 的值是:1507639004441_32000465129702set的值是:1507639004441_32000465141607get 的值是:1507639004441_32000465141607set的值是:1507639004441_32000465153922
阅读全文
0 0
原创粉丝点击