zoukankan      html  css  js  c++  java
  • java-ConcurrentLinkedQueue 简单使用

    import java.util.concurrent.ConcurrentLinkedQueue;
    
    public class CacheTest {
        /**
         *
         * offer(E e) 将指定元素插入此队列的尾部。
         * poll() 获取并移除此队列的头,如果此队列为空,则返回 null。
         * peek() 获取但不移除此队列的头;如果此队列为空,则返回 null。
         * remove(Object o) 从队列中移除指定元素的单个实例(如果存在)。
         * @param args
         */
    
        @SuppressWarnings({ "rawtypes", "unchecked" })
        public static void main(String[] args) {
            ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue();
            queue.offer("哈哈哈");
            System.out.println("offer后,队列是否空?" + queue.isEmpty());
            System.out.println("从队列中poll:" + queue.poll());
            System.out.println("poll后,队列是否空?" + queue.isEmpty());
    
            queue.offer("哈哈哈");
            System.out.println("
    offer后,队列是否空?" + queue.isEmpty());
            System.out.println("从队列中peek:" + queue.peek());
            System.out.println("从队列中peek:" + queue.peek());
            System.out.println("从队列中peek:" + queue.peek());
            System.out.println("peek后,队列是否空?" + queue.isEmpty());
    
            queue.offer("哈哈哈");
            System.out.println("
    offer后,队列是否空?" + queue.isEmpty());
            System.out.println("从队列中remove已存在元素 :" + queue.remove("哈哈哈"));
            System.out.println("从队列中remove不存在元素:" + queue.remove("123"));
            System.out.println("remove后,队列是否空?" + queue.isEmpty());
        }
    
    }
    
    offer后,队列是否空?false
    从队列中poll:哈哈哈
    poll后,队列是否空?true
    
    offer后,队列是否空?false
    从队列中peek:哈哈哈
    从队列中peek:哈哈哈
    从队列中peek:哈哈哈
    peek后,队列是否空?false
    
    offer后,队列是否空?false
    从队列中remove已存在元素 :true
    从队列中remove不存在元素:false
    remove后,队列是否空?false

  • 相关阅读:
    例题3-6 环状序列UVa1584
    习题3-1 Score UVa1585
    Sublime Text3 Python虚拟环境(补充)——解决控制台中文乱码情况
    Cookie保存在本地方法介绍
    Multisim仿真
    小米手机安装charles证书
    【转】缓存
    【转】5种网络IO模型
    【转】分布式锁的几种使用方式(redis、zookeeper、数据库)
    【转】白话解析:一致性哈希算法(consistent hashing)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/9802015.html
Copyright © 2011-2022 走看看