ProduceQueue;支持堵塞和非堵塞

package com.example.zhanghaohao089.mytest;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 实现一个消费队列,有堵塞和非堵塞方法
 *
 * @author ZHANGHAOHAO
 * @date 2017/5/11
 */

public class ProduceQueue<T> {
    private T[] items = null;
    private int head;//头部
    private int last;//尾部
    private Lock lock = new ReentrantLock();
    private Condition notFull = lock.newCondition();
    private Condition notEmpty = lock.newCondition();
    private int count;//目前的count
    private int max;//最大内容

    public ProduceQueue() {
        this(10);
    }

    public ProduceQueue(int max) {
        items = (T[])new Object[max];
        this.max = max;
    }

    //堵塞的
    public void put(T item) {
        lock.lock();
        try {
            if (count == max) {
                notFull.await();
            }
            enqueue(item);
            notEmpty.signalAll();
            count++;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    //非堵塞的
    public boolean offer(T item) {
        lock.lock();
        try {
            if (count == max) {
                return false;
            }
            enqueue(item);
            notEmpty.signalAll();
            count++;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return true;
    }

    //只堵塞一定时间
    public boolean offer(T item, long timeout, TimeUnit unit) {
        lock.lock();
        long nanos = unit.toNanos(timeout);
        try {
            if (count == max) {
                if (nanos <= 0)
                    return false;
                notFull.awaitNanos(nanos);
            }
            enqueue(item);
            notEmpty.signalAll();
            count++;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        return true;
    }

    //非堵塞的
    public T poll() {
        T item = null;
        lock.lock();
        try {
          if (count == 0) {
                return null;
            }
            item = dequeue();
            notFull.signalAll();
            count --;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return item;
    }

    //非堵塞的
    public T poll(long timeout, TimeUnit unit) {
        T item = null;
        lock.lock();
        long nanos = unit.toNanos(timeout);
        try {
            if (count == 0) {
                if (nanos <= 0)
                    return null;
                notEmpty.awaitNanos(nanos);
            }
            item = dequeue();
            notFull.signalAll();
            count --;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return item;
    }

    //堵塞的
    public T take() {
        T item = null;
        lock.lock();
        try {
            if (count == 0) {
                notEmpty.await();
            }
            item = dequeue();
            notFull.signalAll();
            count --;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return item;
    }

    //入队列操作
    private void enqueue(T item) {
        if (last == max) {
            last = 0;
        }
        items[last++] = item;
    }

    //出队列操作
    private T dequeue() {
        T item = items[head];
        items[head] = null;
        if (++head == max) {
            head = 0;
        }
        return item;
    }

    public int getSize() {
        return count;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值