JAVA锁--Condition

2017-03-15  本文已影响27人  小犇手K线研究员

Conndition是什么?

{@code Condition} factors out the {@code Object} monitor

利用conditon实现生产者消费者模型

package com.cy.util.lock;
import com.cy.util.container.CycleArray;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ConditionStudy {
    final Lock lock = new ReentrantLock();
    final Condition producterCondition = lock.newCondition();
    final Condition consumerCondition = lock.newCondition();
    final CycleArray<Integer> queue = new CycleArray<Integer>(10);
    public boolean produce(Integer value) throws InterruptedException {
        lock.lock();
        try {
            while (queue.isFull()) {
                producterCondition.await();
            }
            queue.add(value);
            System.out.println("add : " + value);
            consumerCondition.signal();
            return true;
        } finally {
            lock.unlock();
        }
    }
    public Integer consume() throws InterruptedException {
        lock.lock();
        try {
            while (queue.isEmpty()) {
                consumerCondition.await();
            }
            Integer value = queue.get();
            producterCondition.signal();
            System.out.println("get : " + value);
            return value;
        } finally {
            lock.unlock();
        }
    }
    public static void main(String[] args) {
        final ConditionStudy conditionStudy = new ConditionStudy();
        Thread consumer = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 10000; i++) {
                        conditionStudy.consume();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        consumer.start();
        Thread producer = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 10000; i++) {
                        conditionStudy.produce(i);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();
    }
}

CycleArray.java

package com.cy.util.container;
public class CycleArray<E> {
    private E[] values;
    private int head = -1;
    private int tail = 0;
    private int capacity;
    public CycleArray(int capacity) {
        values = (E[]) new Object[capacity];
        this.capacity = capacity;
    }
    public boolean add(E value) {
        if (head != tail) {
            values[tail] = value;
            if (head == -1) {
                head = tail;
            }
            tail = (tail + 1) % capacity;
            return true;
        } else {
            return false;
        }
    }
    public E get() {
        if (head == -1) {
            return null;
        } else {
            E value = values[head];
            head = (head + 1) % capacity;
            head = head == tail ? -1 : head;
            return value;
        }
    }
    public boolean isEmpty() {
        return head == -1;
    }
    public boolean isFull() {
        return head == tail;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读