Java并发 - J.U.C并发容器类 list、set、queue

简介: Queue API阻塞是通过 condition 来实现的,可参考 Java 并发 - Lock 接口ArrayBlockingQueue 阻塞LinkedBlockingQueue 阻塞ArrayQueue 非阻塞LinkedQueue 非阻塞
  1. List
    ArrayList
    本质就是一个数组
    初识化大小默认为 10
    /**

    • Default initial capacity.
      */
      private static final int DEFAULT_CAPACITY = 10;
      每次扩容后大小变为原大小的 1.5 倍
      private void grow(int minCapacity) {
      // overflow-conscious code
      int oldCapacity = elementData.length;
      int newCapacity = oldCapacity + (oldCapacity >> 1); // 扩容为1.5倍大小
      if (newCapacity - minCapacity < 0)

       newCapacity = minCapacity;
      

      if (newCapacity - MAX_ARRAY_SIZE > 0)

       newCapacity = hugeCapacity(minCapacity);
      

      // minCapacity is usually close to size, so this is a win:
      elementData = Arrays.copyOf(elementData, newCapacity);
      }
      使用 for(Object o : list) 迭代器进行迭代循环的时候不应该对列表 list 进行新增或者删除操作,否则会报 ConcurrentModificationException 异常,原因是因为迭代过程中会检查变量数量和期望的数量是否一致。
      如以下操作就会报错

      int i=0;
      for (Object o : list) {

       if(i==0)  list.add("neco");
       i++;
      

      }
      抛出异常的代码

      final void checkForComodification() {

       if (modCount != expectedModCount)
           throw new ConcurrentModificationException();
      

      }
      LinkedList
      本质就是一个链表,没有什么特殊的内容
      里边的 Node 是支持双向链表的
      private static class Node {
      E item;
      Node next;
      Node prev;

      Node(Node prev, E element, Node next) {

       this.item = element;
       this.next = next;
       this.prev = prev;
      

      }
      }
      CopyOnWriteArrayList
      在写的时候复制了一份出来,然后重新写入数据
      /**

    • Appends the specified element to the end of this list.
      *
    • @param e element to be appended to this list
    • @return {@code true} (as specified by { @link Collection#add})
      */
      public boolean add(E e) {
      final ReentrantLock lock = this.lock;
      lock.lock();
      try {
       Object[] elements = getArray();
       int len = elements.length;
       Object[] newElements = Arrays.copyOf(elements, len + 1);
       newElements[len] = e;
       setArray(newElements);
       return true;
      
      } finally {
       lock.unlock();
      
      }
      }
      确保了读写可以同步进行,但是可能会有脏读的情况
      在多读少写的情况下可以使用
      CopyOnWriteArrayList 容器即写时复制的容器。和 ArrayList 比较, 优点是并发安全 ,缺点有两个:

1、多了内存占用:写数据是 copy 一份完整的数据,单独进行操作。占用双份内存。

2、数据一致性:数据写完之后,其他线程不一定是马上读取到最新内容。

CopyOnWriteArrayList

  1. Set 集合
    和 List 比较:不会重复

实现 原理 特点
HashSet 基于 HashMap 实现 非线程安全
CopyOnWriteArraySet 基于 CopyOnWriteArrayList 线程安全
TreeSet 基于 TreeMap 线程安全,有序,查询快
HashSet
内部的实现本质就是一个 Map(因为 key 值不重复),但是只是使用了 Key,对于 value 无所谓

/**
 * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
 * default initial capacity (16) and load factor (0.75).
 */
public HashSet() {
    map = new HashMap<>();
}


/**
 * Adds the specified element to this set if it is not already present.
 * More formally, adds the specified element <tt>e</tt> to this set if
 * this set contains no element <tt>e2</tt> such that
 * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
 * If this set already contains the element, the call leaves the set
 * unchanged and returns <tt>false</tt>.
 *
 * @param e element to be added to this set
 * @return <tt>true</tt> if this set did not already contain the specified
 * element
 */
public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}

// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();

CopyOnWriteArraySet
内部的本质是一个 CopyOnWriteArrayList,通过判断是否存在来确定是否放入数据

/**

 * Creates an empty set.
 */
public CopyOnWriteArraySet() {
    al = new CopyOnWriteArrayList<E>();
}

/**
 * Adds the specified element to this set if it is not already present.
 * More formally, adds the specified element {@code e} to this set if
 * the set contains no element {@code e2} such that
 * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
 * If this set already contains the element, the call leaves the set
 * unchanged and returns {@code false}.
 *
 * @param e element to be added to this set
 * @return {@code true} if this set did not already contain the specified
 *         element
 */
public boolean add(E e) {
    return al.addIfAbsent(e);
}

/**
 * Appends the element, if not present.
 *
 * @param e element to be added to this list, if absent
 * @return {@code true} if the element was added
 */
public boolean addIfAbsent(E e) {
    Object[] snapshot = getArray();
    return indexOf(e, snapshot, 0, snapshot.length) >= 0 ? false :
        addIfAbsent(e, snapshot);
}

/**
 * A version of addIfAbsent using the strong hint that given
 * recent snapshot does not contain e.
 */
private boolean addIfAbsent(E e, Object[] snapshot) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Object[] current = getArray();
        int len = current.length;
        if (snapshot != current) {
            // Optimize for lost race to another addXXX operation
            int common = Math.min(snapshot.length, len);
            for (int i = 0; i < common; i++)
                if (current[i] != snapshot[i] && eq(e, current[i]))
                    return false;
            if (indexOf(e, current, common, len) >= 0)
                    return false;
        }
        Object[] newElements = Arrays.copyOf(current, len + 1);
        newElements[len] = e;
        setArray(newElements);
        return true;
    } finally {
        lock.unlock();
    }
}

TreeSet
本质是一个 TreeMap,但是也只用到了 Key 值,Value 值没有什么意义。

/**
 * Constructs a new, empty tree set, sorted according to the
 * natural ordering of its elements.  All elements inserted into
 * the set must implement the {<a class="at-user" href="//bbs.h3c.com/user/1458404942316748801">@link </a>Comparable} interface.
 * Furthermore, all such elements must be <i>mutually
 * comparable</i>: {@code e1.compareTo(e2)} must not throw a
 * {@code ClassCastException} for any elements {@code e1} and
 * {@code e2} in the set.  If the user attempts to add an element
 * to the set that violates this constraint (for example, the user
 * attempts to add a string element to a set whose elements are
 * integers), the {@code add} call will throw a
 * {@code ClassCastException}.
 */
public TreeSet() {
    this(new TreeMap<E,Object>());
}

/**
 * Adds the specified element to this set if it is not already present.
 * More formally, adds the specified element {@code e} to this set if
 * the set contains no element {@code e2} such that
 * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
 * If this set already contains the element, the call leaves the set
 * unchanged and returns {@code false}.
 *
 * @param e element to be added to this set
 * @return {@code true} if this set did not already contain the specified
 *         element
 * @throws ClassCastException if the specified object cannot be compared
 *         with the elements currently in this set
 * @throws NullPointerException if the specified element is null
 *         and this set uses natural ordering, or its comparator
 *         does not permit null elements
 */
public boolean add(E e) {
    return m.put(e, PRESENT)==null;
}

// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();

SET 接口没有所谓的有序还是无序。 TreeSet 是有序的,此有序是说读取数据的顺序和插入数据的顺序一样。 HashSet 无序? 此无序说的是读取数据的顺序不一定和插入数据的顺序一样。

  1. Queue
    Queue API
    Queue -队列数据结构的实现。分为阻塞队列和非阻塞队列。下列的蓝色区块,为阻塞队列特有的方法。

Queue API

阻塞是通过 condition 来实现的,可参考 Java 并发 - Lock 接口

ArrayBlockingQueue 阻塞
LinkedBlockingQueue 阻塞
ArrayQueue 非阻塞
LinkedQueue 非阻塞

相关文章
|
8天前
|
Java Linux Maven
java依赖冲突解决问题之容器加载依赖jar包如何解决
java依赖冲突解决问题之容器加载依赖jar包如何解决
|
1天前
|
Java
用JAVA架建List集合为树形结构的代码方法
这段代码定义了一个表示树形结构的 `Node` 类和一个用于构建树形结构的 `TreeController`。`Node` 类包含基本属性如 `id`、`pid`、`name` 和 `type`,以及子节点列表 `children`。`TreeController` 包含初始化节点列表并将其转换为树形结构的方法。通过过滤和分组操作实现树形结构的构建。详情可见:[代码示例链接1](http://www.zidongmutanji.com/zsjx/43551.html),[代码效果参考链接2](https://www.257342.com/sitemap/post.html)。
20 5
|
8天前
|
存储 Java API
【Java高手必备】揭秘!如何优雅地对List进行排序?掌握这几种技巧,让你的代码瞬间高大上!
【8月更文挑战第23天】本文深入探讨了Java中对List集合进行排序的各种方法,包括使用Collections.sort()、自定义Comparator以及Java 8的Stream API。通过示例代码展示了不同情况下如何选择合适的方法:从简单的整数排序到自定义类对象的排序,再到利用Comparator指定特殊排序规则,最后介绍了Stream API在排序操作中的简洁应用。理解这些技术的区别与应用场景有助于提高编程效率。
15 4
|
9天前
|
存储 Java 索引
|
9天前
|
测试技术 索引 Python
Python接口自动化测试框架(基础篇)-- 常用数据类型list&set()
本文介绍了Python中list和set两种数据类型的使用,包括它们的创建、取值、增删改查操作、排序以及内置函数的使用,还探讨了list的比较函数和set的快速去重功能。
12 0
|
10天前
|
Java 测试技术 数据库
容器镜像解析问题之解析 Java 应用依赖时识别 jar 包如何解决
容器镜像解析问题之解析 Java 应用依赖时识别 jar 包如何解决
13 0
|
10天前
|
存储 安全 Java
【Java 第四篇章】流程控制、容器
本文档详细介绍了Java中的流程控制、集合类型、数组声明及容器的声明与遍历等内容。在流程控制部分,包括了if、if...else、if...else if...else、switch等语句的使用方法,并提供了具体示例。接着,文档对比分析了Java中单列集合(如HashSet、LinkedHashSet、TreeSet等)与双列集合(如HashMap、LinkedHashMap、Hashtable等)的特点及底层实现原理。此外,还介绍了如何声明与初始化数组,并提供了多种循环结构的使用示例。最后,通过具体的代码示例展示了不同集合类型的声明、基本操作(如添加、删除、更新、查找)以及遍历方法。
10 0
|
10天前
|
安全 算法 Java
【Java集合类面试二】、 Java中的容器,线程安全和线程不安全的分别有哪些?
这篇文章讨论了Java集合类的线程安全性,列举了线程不安全的集合类(如HashSet、ArrayList、HashMap)和线程安全的集合类(如Vector、Hashtable),同时介绍了Java 5之后提供的java.util.concurrent包中的高效并发集合类,如ConcurrentHashMap和CopyOnWriteArrayList。
【Java集合类面试二】、 Java中的容器,线程安全和线程不安全的分别有哪些?
|
4月前
|
安全 算法 Java
安全无忧:Java并发集合容器的应用与实践
安全无忧:Java并发集合容器的应用与实践
39 0
安全无忧:Java并发集合容器的应用与实践
|
4月前
|
存储 算法 Java
盘点Java集合(容器)概览,Collection和Map在开发中谁用的最多?
盘点Java集合(容器)概览,Collection和Map在开发中谁用的最多?
52 0

热门文章

最新文章

下一篇
云函数