java 传统集合和并发集合的区别

java 传统集合和并发集合的区别

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

  • 传统集合 中的大部分类 (如 ArrayList 、 LinkedList 、 HashMap 等) 都是非同步的,因此不存在线程安全问题。但是,所有存在于并发集合中的类都是同步性质的。因此在并发类中,我们不需要关心线程安全问题。
  • 而传统的集合也有 一些类(如 Vector 、 Stack 等) 是同步的,传统的集合也有 SynchronizedSet、SynchronizedList、SynchronizedMap 方法,通过这些方法我们可以得到非同步对象的同步版本。但是,由于宽锁机制的存在,上述同步类的性能并不理想,而并发集合类的性能相对于传统集合类要高。
  • 在传统的集合中,如果一个线程正在迭代一个集合对象,而另一个线程试图同时在该迭代对象中添加新的元素,那么我们会得到 RuntimeException ConcurrentModificationException。 而在上述情况下,如果我们使用并发集合类,我们将不会得到任何Runtime Exception。
  • 如果我们的应用程序中没有处理线程,传统的集合类是很好的选择,而由于并发/同步集合,我们可以使用多个线程来处理集合对象。因此,如果我们在应用程序中处理多个线程,并发集合是最佳选择。
// 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程