简单队列缓存

public enum SequenceEnum {
    SEQUENCE;

    SequenceEnum() {
    }

    private static final ConcurrentLinkedQueue<String> cache = new ConcurrentLinkedQueue<>();

    private static final ReentrantLock lock = new ReentrantLock();

    /**
     *
     * 出队
     */
    public String poll() {
        try {
            lock.lock();
            if (cache.size() < 100) {
                //必须调用其他方法增加队列数量
                offer(SequenceUtil.getSequenceArray());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return cache.poll();
    }
    /**
     *
     * 入队
     */
    public void offer(String[] sequenceArray) {
        for (String sequence : sequenceArray) {
            cache.add(sequence);
        }
    }

    /**
     *
     * 队列数量太大,不可使用,因为遍历整个队列,耗时过长
     */
    public int size() {
        return cache.size();
    }

    /**
     *
     * 判空使用此方法
     */
    public boolean isEmpty(){
        return cache.isEmpty();
    }

}

猜你喜欢

转载自www.cnblogs.com/huangtao1927/p/11067568.html
今日推荐