博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
锁框架:条件
阅读量:4357 次
发布时间:2019-06-07

本文共 4388 字,大约阅读时间需要 14 分钟。

Condition声明了下列方法:

void await(): 在接收到信号或者被中断之前,强制调用线程一直等待。

long awaitNanos(long nanosTimeout): 在接收到信号,被中断,或者超过指定的等待时间之前,强制当前线程一直等待。

boolean awaitUninterruptibly(): 在接收到信号之前,强制当前线程一直等待。

void signal(): 唤醒一个等待中的线程。

void singalAll(): 唤醒所有等待中的线程。

示例:

package com.lock;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * condition 条件 *     接口condition把object的wait和notification方法(wait(),notify()和notifyAll()) * 分解到不同的条件对象中。通过把这些条件和任意的lock实现使用组合,起到让每个对象上具有多重等待集合的作用。 * 生产者与消费者模型 *     用lock和condition替代synchronize和等待及通知 *     生产一个字母后消费一个字母 * @author Administrator * */public class PC {    public static void main(String[] args) {        Shared s = new Shared();        new Producer(s).start();        new Consumer(s).start();    }}class Shared {    private char c;    private volatile boolean available;    private final Lock lock;    private final Condition condition;    Shared() {        available = false;  //初始化false,保证先生产再消费        lock = new ReentrantLock();        condition = lock.newCondition();  //获取特定lock实例的condition实例    }    Lock getLock() {        return lock;    }    char getSharedChar() {        lock.lock();        try {            while (!available){                try {                    condition.await();                } catch (InterruptedException ie) {                    ie.printStackTrace();                }            }            available = false;            condition.signal();        } finally {            lock.unlock();            return c;        }    }    void setSharedChar(char c) {        lock.lock();        try {            while (available)                try {                    condition.await();                } catch (InterruptedException ie) {                    ie.printStackTrace();                }            this.c = c;            available = true;  //生产一个后,必须先消费            condition.signal();  //唤醒一个等待中的线程        } finally {            lock.unlock();        }    }}//生产者class Producer extends Thread {    private final Lock l;    private final Shared s;    Producer(Shared s) {        this.s = s;        l = s.getLock();    }    @Override    public void run() {        for (char ch = 'A'; ch <= 'Z'; ch++) {            l.lock();            s.setSharedChar(ch);            System.out.println(ch + " produced by producer.");            l.unlock();        }    }}//消费者class Consumer extends Thread {    private final Lock l;    private final Shared s;    Consumer(Shared s) {        this.s = s;        l = s.getLock();    }    @Override    public void run() {        char ch;        do {            l.lock();            ch = s.getSharedChar();            System.out.println(ch + " consumed by consumer.");            l.unlock();        } while (ch != 'Z');    }}

 传统方式实现以上代码:通过synchronize和等待及通知实现

package com.lock;/** * 生产者消费者模式 *     生产一个字母后消费一个字母 * @author Administrator * */public class PC2 {    public static void main(String[] args) {        Shared2 s = new Shared2();        new Producer2(s).start();        new Consumer2(s).start();    }}class Shared2 {    private char c;    private volatile boolean writeable = true;    synchronized void setSharedChar(char c) {        while (!writeable)            try {                wait();            } catch (InterruptedException ie) {            }        this.c = c;        writeable = false;        notify();    }    synchronized char getSharedChar() {        while (writeable)            try {                wait();            } catch (InterruptedException ie) {            }        writeable = true;        notify();        return c;    }}class Producer2 extends Thread {    private final Shared2 s;    Producer2(Shared2 s) {        this.s = s;    }    @Override    public void run() {        for (char ch = 'A'; ch <= 'Z'; ch++) {            synchronized (s) {                s.setSharedChar(ch);                System.out.println(ch + " produced by producer.");            }        }    }}class Consumer2 extends Thread {    private final Shared2 s;    Consumer2(Shared2 s) {        this.s = s;    }    @Override    public void run() {        char ch;        do {            synchronized (s) {                ch = s.getSharedChar();                System.out.println(ch + " consumed by consumer.");            }        } while (ch != 'Z');    }}

 

转载于:https://www.cnblogs.com/x-jingxin/p/10678489.html

你可能感兴趣的文章
08、内建函数
查看>>
Glibc 与 libc 的区别和联系
查看>>
hdu 1032 The 3n + 1 problem
查看>>
380. Insert Delete GetRandom O(1)
查看>>
电路相关知识--读<<继电器是如何成为CPU的>>
查看>>
你在职场中值多少钱?
查看>>
angular风格指南
查看>>
Unity UGUI烟雾效果
查看>>
[JavaScript]Promise
查看>>
类型转换(2)
查看>>
BZOJ 1016--[JSOI2008]最小生成树计数(kruskal&搜索)
查看>>
326. Power of Three
查看>>
Debugging Custom SharePoint Timer Jobs
查看>>
实验四 恶意代码技术
查看>>
让 Winform 窗口悬浮的简单方式
查看>>
TcxGrid
查看>>
Python——day02
查看>>
微软的官方技术文档
查看>>
ubuntu
查看>>
一款JavaScript开发的扫雷小游戏
查看>>