`
ryhome
  • 浏览: 44809 次
  • 性别: 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 concurrency集合之ConcurrentSkipListSet_动力节点Java学院整理

    * 带Comparator参数的构造方法:ConcurrentSkipListSet(Comparator&lt;? super E&gt; comparator) * 带SortedSet参数的构造方法:ConcurrentSkipListSet(SortedSet&lt;E&gt; s) ConcurrentSkipListSet还提供了多种操作方法,...

    java集合-ConcurrentSkipListSet的使用

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

    Java基础学习25.pdf

    2. **add(E e)**:向HashSet添加元素时,实际上是将元素作为键添加到了内部的HashMap中,如果键已经存在,则add操作会返回false。 3. **iterator()**:遍历HashSet元素时,实际上是在遍历内部HashMap的键集合。 4....

    Java_Collection_List-Set-Map.zip_list set map

    - List特有的方法有add(E element, int index)用于在指定位置插入元素,get(int index)获取指定位置的元素,remove(int index)移除指定位置的元素等。 - Set特有的方法是contains(Object o)检查集合是否包含特定...

    JAVA学习笔记第十三天示例代码

    它的常用API如`add()`用于向集合中添加元素,`remove()`用于移除元素,`size()`返回集合中元素的数量,以及`contains()`用于检查集合中是否存在特定元素。ArrayList和LinkedList是List接口的两种常见实现,分别提供...

    Java并发编程与高并发解决方案之并发容器(J.U.C).docx

    // 此方法不会立即终止线程运行,而是要等已开启的线程全部执行完关闭 } private static void test(int threadNum) throws Exception { Thread.sleep(100); log.info("{}", threadNum); Thread.sleep(100); ...

    jdk api 1.8 中文版

    Java 1.8还增加了新的数据结构,如TreeSet和TreeMap的子类ConcurrentSkipListSet和ConcurrentSkipListMap,提供了并发访问的性能优化。 九、动态代理增强 Java 1.8增强了动态代理,支持接口中的默认方法,使得动态...

    Iterator的使用

    3. **并发迭代器**:在多线程环境下,Java的`ConcurrentSkipListSet`和`ConcurrentLinkedQueue`等并发集合类提供了`iterator()`方法,返回的迭代器能够安全地在并发环境中使用。 总之,迭代器是访问聚合对象的常用...

    java se 1.8 中文 api

    8. **并发更新集合类**:如`ConcurrentHashMap`的改进,以及新的`ConcurrentSkipListMap`和`ConcurrentSkipListSet`,提供了高效且线程安全的集合操作。 9. **接口私有方法和静态方法**:Java 8允许接口中定义私有...

    jdk1.8 中文API.7z

    **JDK1.8 中文API** 是Java开发者的重要参考资料,它包含了JDK1.8版本的所有类、接口和方法的中文解释,便于中国开发者理解和使用。这份文档是基于原始的英文API文档进行翻译,结合了百度和谷歌的翻译技术,以提供更...

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

    - 其他标准的Set接口方法,如`add()`, `addAll()`, `clear()`, `contains()`, `containsAll()`, `equals()`, `isEmpty()`, `iterator()`, `remove()`, `removeAll()`, `retainAll()`等,也都被覆写以保证线程安全。...

    jdk1.8 api中文版Google翻译.zip

    8. 并发更新的集合:Java 8对并发集合进行了增强,如`ConcurrentHashMap`现在支持更多的操作,`ConcurrentSkipListMap`和`ConcurrentSkipListSet`也提供了更多的线程安全的选项。 9. Type注解:Java 8增加了类型...

    JDK_1.8帮助文档

    此外,`ConcurrentSkipListMap`和`ConcurrentSkipListSet`也提供了线程安全的有序集合操作。 **6. Date和Time API的重构** 旧的`java.util.Date`和`java.util.Calendar`类在JDK 1.8中被新的`java.time`包取代。新...

    Java常见的线程安全的类.docx

    此外,还有ConcurrentSkipListSet和ConcurrentSkipListMap等。 6. ThreadPoolExecutor:这是Java的线程池实现,它使用ReentrantLock进行同步,管理线程的执行和任务调度,保证并发环境下的正确性。 7. Collections...

    [线程技术]排序对象

    需要注意的是,虽然这个例子中使用了线程安全的 `List`,但在实际应用中,如果多线程环境中的排序操作是独立的,可以考虑使用并发集合,如 `ConcurrentSkipListSet` 或 `ConcurrentLinkedQueue`,它们在性能上可能更...

    JAVA并发容器代码随读1

    在`java.util.concurrent`包中,有四种主要的并发容器类型:队列(BlockingQueue)、Map(ConcurrentMap)、Set(ConcurrentSkipListSet和CopyOnWriteArraySet)以及List(CopyOnWriteArrayList)。这些容器的设计...

    66丨迭代器模式(中):遍历集合的同时,为什么不能增删集合元素?1

    迭代器模式是一种设计模式,它的主要作用是在不暴露容器内部结构的情况下,提供一种遍历容器内元素的方法。这种模式使得代码解耦,提高了灵活性。在Java等编程语言中,迭代器通常通过`Iterator`接口来实现,允许用户...

    Java6 Collection Framework 新特性概览.pdf

    - 提供插入、移除和检查元素的方法,这些方法有两种形式:一种在失败时抛出异常,另一种则返回特殊值(`null` 或 `false`)。 - 可以作为FIFO队列或LIFO栈使用。 - 不支持索引访问元素。 - 推荐阻止 `null` 元素...

    (TreeSet) s.subSet(608, true, 611, true)

    7. **并发考虑**:如果在多线程环境中使用TreeSet,需要使用ConcurrentSkipListSet,因为TreeSet不是线程安全的。 8. **自定义排序**:通过传递Comparator实例给TreeSet构造函数,可以自定义元素的排序方式。 9. *...

    java中集合排序

    - 对于并发环境下的排序,Java提供了`ConcurrentSkipListSet`,它是一个线程安全的`SortedSet`实现,使用跳表结构支持高效的并发排序。 总之,Java中集合排序涉及到多个接口、类和方法。理解并熟练运用这些工具,...

Global site tag (gtag.js) - Google Analytics