双队列缓存

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DoubleBufferList<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(DoubleBufferList.class);

    private static final long ONE_SECOND = 1000L;

    private static final int MAX_INDEX = 1000;

    private long index = 0l;

    private AtomicInteger idx = new AtomicInteger(0);

    private final Lock lock = new ReentrantLock();
    private final Condition notEmpty = lock.newCondition();

    private List<T> list0 = new ArrayList<T>();
    private List<T> list1 = new ArrayList<T>();
    @SuppressWarnings("unchecked")
    private List<T>[] list = new List[] { list0, list1 };

    public DoubleBufferList() {
    }

    public List<T> getActiveList(AtomicBoolean quitFlag) {
        List<T> list = null;
        while (quitFlag.get()) {
            try {
                lock.lock();
                list = getActiveList();
                if (list.isEmpty()) {
                    list = null;
                    boolean flag = notEmpty.await(ONE_SECOND, TimeUnit.MILLISECONDS);
                } else {
                    break;
                }

            } catch (Exception e) {
                LOGGER.warn(e.getMessage(), e);
            } finally {
                lock.unlock();
            }
        }
        if (list == null) {
            list = new ArrayList<T>();
        }
        return list;
    }

    protected List<T> getActiveList() {
        idx.compareAndSet(MAX_INDEX, 0);
        int index = idx.incrementAndGet();
        return list[(index - 1) % 2];
    }

    protected List<T> getDeactiveList() {
        return list[idx.get() % 2];
    }

    public boolean checkActiveList() {
        if (list[(idx.get() + 1) % 2].size() == 0) {
            return false;
        } else {
            return true;
        }

    }

    public int getActiveListSize() {
        return list[(idx.get() + 1) % 2].size();
    }

    public List<T> getActiveList(int number) {
        List<T> returnlist = new ArrayList<T>();
        try {
            lock.lock();
            List<T> list1 = list[(idx.get() + 1) % 2];
            for (int i = 0; i <= (list1.size() > number ? number - 1 : list1.size() - 1); i++) {
                returnlist.add(list1.get(i));
            }
            list1.removeAll(returnlist);
        } catch (Exception e) {
            LOGGER.warn(e.getMessage(), e);
        } finally {
            lock.unlock();
        }
        return returnlist;
    }

    public List<T> getList0() {
        return list0;
    }

    public List<T> getList1() {
        return list1;
    }

    public List<T> getDList() {
        return getDeactiveList();
    }

    /**
     * 将数据向未使用中的list中放
     * 
     * @param data
     * 
     */
    public void addData(T data) {
        lock.lock();
        try {
            getDeactiveList().add(data);
            // index++;
            notEmpty.signal();
        } finally {
            lock.unlock();
        }
    }

    public boolean checkUnActiveList() {
        if (list[(idx.get()) % 2].size() == 0) {
            return false;
        } else {
            return true;
        }

    }

    public int getListSize0() {
        return this.list0.size();
    }

    public int getListSize1() {
        return this.list1.size();
    }

    protected List<T> getDataList() {
        return list[(int) (index % 2l)];
    }
}

以上为双队列缓存源码

List<String> list = deviceSubscribeList.getData().getActiveList(1);
        if (!deviceSubscribeList.getData().checkActiveList()) {
            if (deviceSubscribeList.getData().checkUnActiveList()) {
                deviceSubscribeList.getData().getActiveList(d);
            }
        }

缓存队列使用

@Service
public class PassSubscribeList {
    
    private DoubleBufferList<String> data=new DoubleBufferList<String>();

    public DoubleBufferList<String> getData() {
        return data;
    }
    public void addData(String d){
        data.addData(d);
    }
}
posted @ 2017-12-29 17:24  暗渡陈仓xy  阅读(782)  评论(0编辑  收藏  举报