BlockingQueue实现类<二>

本文深入分析了Java并发包中LinkedBlockingQueue的实现原理,重点介绍了其构造方法、offer方法及take方法的具体实现,并通过对比ArrayBlockingQueue突出了其基于链表实现的特点。

这篇文章主要讲LinkedBlockingQueue。阻塞队列使用链表来完成存储,之前分析的ArrayBlockingQueue是用数组。现在看下具体实现会有什么特点,还是从构造方法出发。

   public LinkedBlockingQueue(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        //初始化了一个Node。
        last = head = new Node<E>(null);
    }

跟之前一样,来分析下offer吧。

   public boolean offer(E e) {
        if (e == null) throw new NullPointerException();
        final AtomicInteger count = this.count;
        if (count.get() == capacity)
            return false;
        int c = -1;
        Node<E> node = new Node<E>(e);
        final ReentrantLock putLock = this.putLock;
        putLock.lock();
        try {
            if (count.get() < capacity) {
                enqueue(node);
                c = count.getAndIncrement();
                //再加1,还小于容量,可以释放锁。
                if (c + 1 < capacity)
                    notFull.signal();
            }
        } finally {
            putLock.unlock();
        }
        if (c == 0)
            signalNotEmpty();
        return c >= 0;
    }

//将node 放在链尾的末端。

   private void enqueue(Node<E> node) {
        // assert putLock.isHeldByCurrentThread();
        // assert last.next == null;
        last = last.next = node;
    }

//获取head的内容。

public E take() throws InterruptedException {
        E x;
        int c = -1;
        final AtomicInteger count = this.count;
        //写锁。
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lockInterruptibly();
        try {
        //没有内容,需要等待。
            while (count.get() == 0) {
                notEmpty.await();
            }
            x = dequeue();
            c = count.getAndDecrement();
            //释放notEmpty锁。
            if (c > 1)
                notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
        if (c == capacity)
            signalNotFull();
        return x;
    }

来分析下 dequeue()。链表操作。返回了head结点并且head置为空,释放掉。注意写法很考究。

private E dequeue() {
        // assert takeLock.isHeldByCurrentThread();
        // assert head.item == null;
        Node<E> h = head;
        Node<E> first = h.next;
        //这样原来的头部结点的next也指向自身了。原来next持有别的对象,这样容易gc释放。
        h.next = h; // help GC
        //head变为了原来的next。
        head = first;
        E x = first.item;
        first.item = null;
        return x;
    }

其实跟之前分析的ArrayBlockingQueue差别不大。不过是读写分离,利用链表实现。

public interface CacheService { @Nullable <T> T get(String cacheName, @NonNull String key, @NonNull Class<T> clazz); /** @deprecated */ @Nullable @Deprecated <T> T get(String cacheName, @NonNull String key, @NonNull BiFunction<String, String, T> loadHandle, @NonNull Class<T> clazz); Stream<String> getKeysByPattern(String cacheName, @NonNull String pattern); <T> void set(String cacheName, @NonNull String key, @NonNull T value); <T> void set(String cacheName, @NonNull String key, @NonNull T value, long timeToLive, @NonNull TimeUnit timeUnit); boolean deleteKey(String cacheName, @NonNull String key); long deleteKeyByPrefix(String cacheName, @NonNull String prefix) throws UnsupportedOperationException; boolean expireKey(String cacheName, @NonNull String key, long timeToLive, @NonNull TimeUnit timeUnit); <T> boolean replace(String cacheName, @NonNull String key, @NonNull T oldValue, @NonNull T newValue); boolean cas(String cacheName, @NonNull String key, @NonNull Version value); long getRemainTimeToLive(String cacheName, @NonNull String key); IAtomicLong getAtomicLong(String cacheName, @NonNull String key); <T> Map<String, T> getHash(String cacheName, @NonNull String key, @NonNull Class<T> clazz); @Nullable <T> T getHashValue(String cacheName, @NonNull String key, @NonNull String hashKey, @NonNull Class<T> clazz); <T> T putHashValue(String cacheName, @NonNull String key, @NonNull String hashKey, @NonNull T value); @Nullable <T> T deleteHashValue(String cacheName, @NonNull String key, @NonNull String hashKey); /** @deprecated */ @Deprecated <T> Map<String, T> getTimedHash(String cacheName, @NonNull String key, @NonNull Class<T> clazz); /** @deprecated */ @Deprecated @Nullable <T> T getTimedHashValue(String cacheName, @NonNull String key, @NonNull String hashKey, @NonNull Class<T> clazz); /** @deprecated */ @Deprecated <T> T putTimedHashValue(String cacheName, @NonNull String key, @NonNull String hashKey, @NonNull T value); /** @deprecated */ @Deprecated <T> T putTimedHashValue(String cacheName, @NonNull String key, @NonNull String hashKey, @NonNull T value, long timeToLive, @NonNull TimeUnit timeUnit); /** @deprecated */ @Deprecated @Nullable <T> T deleteTimedHashValue(String cacheName, @NonNull String key, @NonNull String hashKey); <T> boolean addSortedSetValue(String cacheName, @NonNull String key, @NonNull T value, double score); <T> int addSortedSetValueReturnSize(String cacheName, @NonNull String key, @NonNull T value, double score); <T> Collection<T> rangeSortedByScore(String cacheName, @NonNull String key, double min, double max, @NonNull Class<T> clazz); <T> boolean removeSortedSetValue(String cacheName, @NonNull String key, @NonNull T value); <T> Collection<T> pollSortedByScore(String cacheName, @NonNull String key, double min, double max, @NonNull Class<T> clazz); <T> Collection<T> pollSortedByScore(String cacheName, @NonNull String key, double min, double max, int count, @NonNull Class<T> clazz); <T> Collection<T> pollFirst(String cacheName, @NonNull String key, int count, @NonNull Class<T> clazz); <T> Collection<T> pollFirst(String cacheName, @NonNull String key, int count, double min, double max, @NonNull Class<T> clazz); @Nullable <T> T peekFirst(String cacheName, @NonNull String key, @NonNull Class<T> clazz); int getSortedSetSize(String cacheName, @NonNull String key); <T> Set<T> getSet(String cacheName, @NonNull String key, @NonNull Class<T> clazz); <T> List<T> getList(String cacheName, @NonNull String key, @NonNull Class<T> clazz); <T> Queue<T> getQueue(String cacheName, @NonNull String key, @NonNull Class<T> clazz); <T> Deque<T> getDeque(String cacheName, @NonNull String key, @NonNull Class<T> clazz); <T> BlockingQueue<T> getBlockingQueue(String cacheName, @NonNull String key, @NonNull Class<T> clazz); <T> BlockingDeque<T> getBlockingDeque(String cacheName, @NonNull String key, @NonNull Class<T> clazz); } 这是项目里面的缓存接口,我实现的部分(get)有点问题,如下,怎么修改: @Slf4j @Service public class WebSocketResultCacheService { public static final String WEBSOCKET_RESULT = "websocket:result"; private static final int FILE_ID_TIME_OUT = 30; private static final TimeUnit FILE_ID_TIME_OUT_UNIT = TimeUnit.MINUTES; @Autowired private CacheService cacheService; @Autowired private ObjectMapper objectMapper; // Jackson ObjectMapper /** * @param vmsId vmsId * @param taskId taskId * @return */ public <T> getResult(String vmsId, String taskId) { String cacheKey = buildCacheKey(vmsId, taskId); return cacheService.get(WEBSOCKET_RESULT, cacheKey, ); } /** * @param vmsId vmsId * @param taskId taskId * @param dto WebSocketResultDTO */ public <T> void setValue(String vmsId, String taskId, WebSocketResultDTO<T> dto) { String cacheKey = buildCacheKey(vmsId, taskId); // todo 修改缓存名字 RedisKey.WEBSOCKET_RESULT cacheService.set(WEBSOCKET_RESULT, cacheKey, dto, FILE_ID_TIME_OUT, FILE_ID_TIME_OUT_UNIT); } /** * @param vmsId vmsId * @param taskId taskId */ public void delete(String vmsId, String taskId) { String cacheKey = buildCacheKey(vmsId, taskId); // todo 修改缓存名字 RedisKey.WEBSOCKET_RESULT cacheService.deleteKey(WEBSOCKET_RESULT, cacheKey); } private String buildCacheKey(String vmsId, String taskId) { return String.format("websocket:result:%s:%s", vmsId, taskId); } }
10-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值