java多核并行同步_Java中同步收集和并行收集之间的区别

java多核并行同步

Java中的同步集合与并发集合 (Synchronized Collection vs Concurrent Collection in Java)

Here, we will see how Synchronized Collection differs from Concurrent Collection in Java?

在这里,我们将看到同步集合与Java中的并发集合有何不同?

同步收集 (Synchronized Collection)

  • Now, we will see what is synchronize? Here, synchronize means only one thread is allowed to operate on an object at a time or in other words the object(which is synchronized) can't be modified by multiple threads simultaneously.

    现在,我们将看到什么是同步的? 在这里,同步意味着一次只允许一个线程在一个对象上操作,换句话说,该对象(已同步)不能同时被多个线程修改。

  • Synchronized Collection can be modified by one thread at a time(i.e. it is not possible to modify or access Synchronized Collection by multiple threads simultaneously).

    同步集合可以一次由一个线程修改(即,不可能同时由多个线程修改或访问同步集合)。

  • Synchronized Collection has low performance than Concurrent Collection because at a time only one thread is allowed to operate on an object so it increases the waiting time of the threads.

    同步收集的性能比并发收集低,因为一次只允许一个线程在一个对象上运行,因此它增加了线程的等待时间。

  • Synchronized Collection acquires the lock on the entire Collection object which provides thread-safety.

    Synchronized Collection获取整个提供了线程安全性的Collection对象的锁。

  • SynchronizedMap is a static inner class of Collections class which is available in java.util.Collections.

    SynchronizedMap是Collections类的静态内部类,可在java.util.Collections中使用。

  • In SynchronizedMap it acquires the lock on entire Map object and it wraps all the methods of Map interface with synchronized keyword.

    SynchronizedMap中,它获取对整个Map对象的锁定,并使用synced关键字包装Map接口的所有方法。

  • SynchronizedMap may allow null keys and null values depend on the real Collections class.

    SynchronizedMap可能允许空键和空值取决于实际的Collections类。

Example:

例:

import java.util.*;

class SynchronizedCollectionClass {
    public static void main(String[] args) {
        try {
            Set set = new HashSet();
            
            set.add(10);
            set.add(20);
            set.add(30);
            set.add(40);
            set.add(50);
            
            System.out.println("Current Set is :" + set);
            Collection collection = Collections.synchronizedCollection(set);
            System.out.println("Synchronized Collection is :" + set);
        } catch (IndexOutOfBoundsException ex) {
            System.out.println("Exception :" + ex.getMessage());
        }
    }
}

Output

输出量

E:\Programs>javac SynchronizedCollectionClass.java

E:\Programs>java SynchronizedCollectionClass
Current Set is :[50, 20, 40, 10, 30]
Synchronized Collection is :[50, 20, 40, 10, 30]

Here, we will see how Concurrent Collection differs from Synchronized Collection in Java?

在这里,我们将看到Concurrent Collection与Java中的Synchronized Collection有何不同?

并发收集 (Concurrent Collection)

  • Now, we will see what is Concurrent? Here, concurrent means only multiple threads are allowed to operate on an object at a time or in other words the object (which is concurrent) can be modified by multiple threads simultaneously.

    现在,我们将看到什么是并发? 在这里,并发意味着一次只允许多个线程对一个对象进行操作,换句话说,该对象(并发)可以同时被多个线程修改。

  • Concurrent Collection can be modified by multiple threads at a time(i.e. it is possible to modify or access Concurrent Collection by multiple threads simultaneously).

    并发集合可以一次由多个线程修改(即,可以同时由多个线程修改或访问并发集合)。

  • Concurrent Collection has high performance than Synchronized Collection because at a time multiple threads are allowed to operate on an object so it decreases the waiting time of the threads.

    并发收集同步 收集具有更高的性能,因为一次允许多个线程在一个对象上运行,因此减少了线程的等待时间。

  • More than one threads can perform read-write operation concurrently still it provides Thread Safety.

    多个线程可以同时执行读写操作,但仍提供线程安全性。

  • ConcurrentHashMap is a class introduced in Java 5 that is available in java.util package.

    ConcurrentHashMap是Java 5中引入的类,可通过java.util包获得。

  • ConcurrentHashMap divides Map object into different parts and every thread acquires the lock on every part.

    ConcurrentHashMap将Map对象分为不同的部分,每个线程都获得每个部分的锁。

  • As we know default concurrency level is 16 that means maximum 16 threads are allowed to access an object simultaneously by default and we can increase and decrease concurrency level if we want.

    我们知道默认的并发级别是16,这意味着默认情况下最多允许16个线程同时访问一个对象,如果需要,我们可以增加和减少并发级别。

  • ConcurrentHashMap does not allow null keys and null values.

    ConcurrentHashMap不允许使用空键和空值。

Example:

例:

// We will see in a program why Concurrent concept is required

import java.util.*;

class ConcurrentClass extends Thread {
    static LinkedList ll = new LinkedList();
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            System.out.println("ConcurrentClass" +
                " will add element");
        }

        // By using add() method to add an element in Concurrent Class Thread
        ll.add("Java");
    }

    public static void main(String[] args)
    throws InterruptedException {
        ll.add("C");
        ll.add("C++");
        ll.add("Ruby");
        ll.add("Python");

        // Creating an instance of ConcurrentClass and it will modify 
        ConcurrentClass cc = new ConcurrentClass();
        cc.start();

        // Iterating LinkedList 
        Iterator iterator = ll.iterator();
        while (iterator.hasNext()) {
            String str = (String) iterator.next();
            System.out.println(str);
            Thread.sleep(8000);
        }
        System.out.println(ll);
    }
}

Output

输出量

E:\Programs>javac ConcurrentClass.java

E:\Programs>java ConcurrentClass
C
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification
        (LinkedList.java:953)
        at java.util.LinkedList$ListItr.next(LinkedList.java:886)
        at ConcurrentClass.main(ConcurrentClass.java:34)


翻译自: https://www.includehelp.com/java/differences-between-synchronized-collection-and-concurrent-collection-in-java.aspx

java多核并行同步

你可能感兴趣的:(java多核并行同步_Java中同步收集和并行收集之间的区别)