📜  Java中传统集合和并发集合的区别

📅  最后修改于: 2022-05-13 01:54:46.140000             🧑  作者: Mango

Java中传统集合和并发集合的区别

我们都知道传统集合(即List、Set、Queue及其实现的类)和并发集合(即ConcurrentMap接口、ConcurrentHashMap类、CopyOnWriteArrayList类等)。在这两个集合中,几乎没有区别,例如:

  • 传统集合中存在的大多数类(即 ArrayList、LinkedList、HashMap 等)本质上是非同步的,因此没有线程安全性。但是并发集合中存在的所有类本质上都是同步的。因此在并发类中,我们不必关心线程安全。
  • 虽然传统集合也有一些本质上同步的类(如 Vector、Stack 等) ,而传统集合也有SynchronizedSet、SynchronizedList、SynchronizedMap方法,通过它们我们可以获得非同步对象的同步版本。但是上面这些 Synchronized 类由于宽锁机制的原因在性能方面并不好。而 Concurrent Collections 类的性能相对于传统 Collections 类。
  • 在传统集合中,如果一个线程正在迭代一个 Collection 对象,并且如果另一个线程同时尝试在该迭代对象中添加新元素,那么我们将得到RuntimeException ConcurrentModificationException 。而在上述情况下,如果我们使用并发集合类,我们将不会收到任何运行时异常。
  • 如果我们不处理应用程序中的线程,传统的 Collections 类是不错的选择。而由于并发/同步集合,我们可以使用多个处理集合对象的线程。因此,如果我们在应用程序中处理多线程,并发集合是最佳选择。
// Java program to illustrate Traditional 
// Collections Problem
import java.util.*;
class ConcurrentDemo extends Thread {
    static ArrayList l = new ArrayList();
    public void run()
    {
        try {
            Thread.sleep(2000);
        }
        catch (InterruptedException e) {
            System.out.println("Child Thread"
                    + " going to add element");
        }
  
        // Child thread trying to add new
        // element in the Collection object
        l.add("D");
    }
  
    public static void main(String[] args)
        throws InterruptedException
    {
        l.add("A");
        l.add("B");
        l.add("c");
  
        // We create a child thread that is
        // going to modify ArrayList l.
        ConcurrentDemo t = new ConcurrentDemo();
        t.start();
  
        // Now we iterate through the ArrayList
        // and get exception.
        Iterator itr = l.iterator();
        while (itr.hasNext()) {
            String s = (String)itr.next();
            System.out.println(s);
            Thread.sleep(6000);
        }
        System.out.println(l);
    }
}

输出:

Exception in thread “main” java.util.ConcurrentModificationException
// Java program to illustrate ConcurrentCollection uses
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
class ConcurrentDemo extends Thread {
    static CopyOnWriteArrayList l = 
                     new CopyOnWriteArrayList();
    public void run()
    {
        try {
            Thread.sleep(2000);
        }
        catch (InterruptedException e) {
            System.out.println("Child Thread"
                     + " going to add element");
        }
  
        // Child thread trying to add new
        // element in the Collection object
        l.add("D");
    }
  
    public static void main(String[] args)
        throws InterruptedException
    {
        l.add("A");
        l.add("B");
        l.add("c");
  
        // We create a child thread that is
        // going to modify ArrayList l.
        ConcurrentDemo t = new ConcurrentDemo();
        t.start();
  
        // Now we iterate through the ArrayList
        // and get exception.
        Iterator itr = l.iterator();
        while (itr.hasNext()) {
            String s = (String)itr.next();
            System.out.println(s);
            Thread.sleep(6000);
        }
        System.out.println(l);
    }
}

输出:

A
B
c