`
ryhome
  • 浏览: 44458 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

ConcurrentSkipListSet的add(E e)方法注释误人子弟!

    博客分类:
  • Java
阅读更多

一个项目用到ConcurrentSkipListSet.add(E e)方法,过程中总觉得元素添加数量有问题,故调试之。


查看add()方法的javadoc,其注释为:
如果此 set 中不包含指定元素,则添加指定元素。更确切地讲,如果此 set 不包含满足 e.equals(e2) 的元素
e2,则向 set 中添加指定的元素 e。如果此 set 已经包含该元素,则调用不更改该 set 并返回
false。



根据注释,只要元素的equals()方法判断不相等就能加入到Set中,可调试发现不是这么回事:

 

public boolean add(E e) {
    return m.putIfAbsent(e, Boolean.TRUE) == null;
}

 
m为 ConcurrentSkipListMap,它的putIfAbsent()方法实现是:

    public V putIfAbsent(K key, V value) {
        if (value == null)
            throw new NullPointerException();
        return doPut(key, value, true);
    }

    private V doPut(K kkey, V value, boolean onlyIfAbsent) {
        Comparable super K> key = comparable(kkey);
        for (;;) {
            Node b = findPredecessor(key);
            Node n = b.next;
            for (;;) {
                if (n != null) {
                    Node f = n.next;
                    if (n != b.next)               // inconsistent read
                        break;;
                    Object v = n.value;
                    if (v == null) {               // n is deleted
                        n.helpDelete(b, f);
                        break;
                    }
                    if (v == n || b.value == null) // b is deleted
                        break;
                    int c = key.compareTo(n.key);
                    if (c > 0) {
                        b = n;
                        n = f;
                        continue;
                    }
                    if (c == 0) {
                        if (onlyIfAbsent || n.casValue(v, value))
                            return (V)v;
                        else
                            break; // restart if lost race to replace value
                    }
                    // else c  z = new Node(kkey, value, n);
                if (!b.casNext(n, z))
                    break;         // restart if lost race to append to b
                int level = randomLevel();
                if (level > 0)
                    insertIndex(z, level);
                return null;
            }
        }
    }
 实际上Set中的添加是根据元素的compareTo()方法在比较!如果compareTo()返回0,就认为2个元素相等,跟equals()方法根本没有关系!JAVA SDK 的JavaDoc也不靠谱啊~~

 

 

0
1
分享到:
评论
1 楼 tsyj810883979 2011-10-21  
/**
     * The underlying map. Uses Boolean.TRUE as value for each
     * element.  This field is declared final for the sake of thread
     * safety, which entails some ugliness in clone()
     */
    private final ConcurrentNavigableMap<E,Object> m;

    /**
     * Constructs a new, empty set that orders its elements according to
     * their {@linkplain Comparable natural ordering}.
     */
    public ConcurrentSkipListSet() {
        m = new ConcurrentSkipListMap<E,Object>();
    }

    /**
     * Constructs a new, empty set that orders its elements according to
     * the specified comparator.
     *
     * @param comparator the comparator that will be used to order this set.
     *        If <tt>null</tt>, the {@linkplain Comparable natural
     *        ordering} of the elements will be used.
     */
    public ConcurrentSkipListSet(Comparator<? super E> comparator) {
        m = new ConcurrentSkipListMap<E,Object>(comparator);
    }
JDK源码中的注释

相关推荐

    java集合-ConcurrentSkipListSet的使用

    ConcurrentSkipListSet 是Java中的一个线程安全的有序集合类,它基于跳表(Skip List)数据结构实现。 下面是关于 ConcurrentSkipListSet 的一些重要信息: 线程安全性:ConcurrentSkipListSet 是线程安全的,可以...

    Java concurrency集合之ConcurrentSkipListSet_动力节点Java学院整理

    主要为大家详细介绍了Java concurrency集合之ConcurrentSkipListSet的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    集合效率不完全皮恩车

    public static char[] chars = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'} ; 比较对象 List接口 AttributeList , ArrayList , CopyOnWriteArrayList , LinkedList RoleList , ...

    Java并发包讲解

    2.HashSet,TreeSet -- CopyONWriteArraySet,ConcurrentSkipListSet 3.hashMap , treeMap -- ConcurrentHashMap,ConcurentSkipListMap(key有序,支持更高并发) ## Concurrent同步工具类 countDownLatch ...

    Java并发编程(学习笔记).xmind

    ConcurrentSkipListSet 替代同步的SortedSet Java 5 Java 6 同步工具类 闭锁 *应用场景 (1)确保某个计算在其需要的所有资源都被初始化后才能继续执行 (2)确保某个服务...

    并发编程笔记20190526.docx

    第一章 线程基础、线程之间...1. ConcurrentSkipListMap和ConcurrentSkipListSet: 60 2. 写时复制容器 60 五、阻塞队列 60 第五章 JMH性能测试 62 1、 JMH环境搭建 62 2、 执行 63 3、 基本概念 63 4、 注解与选项 63

    高级java笔试题-tip:自写博客、博客收藏、开源项目收藏

    线程安全set:ConcurrentSkipListSet、CopyOnWriteArraySet、ConcurrentHashMap.newKeySet()、Collections.synchronizedSet(set) 分布式技术 缓存穿透: 请求去查询缓存数据库中根本就不存在的数据。解决方案: 缓存...

Global site tag (gtag.js) - Google Analytics