同步ArrayList (Synchronizing ArrayList)
In java, there are two ways to synchronize ArrayList,
在Java中,有兩種同步ArrayList的方法,
With the help of synchronizedList() method
借助syncedList()方法
With the help of CopyOnWriteArrayList<T> method
借助CopyOnWriteArrayList <T>方法
1)使用syncedList(列表列表)方法同步ArrayList (1) Synchronizing ArrayList using synchronizedList(List list) method)
This method is available in java.util package.
此方法在java.util包中可用。
With the help of this method, we can make ArrayList synchronized.
借助此方法,我們可以使ArrayList同步。
This is a static method, it is accessible with the class name too. (i.e. If we try to access with the class object, in that case, we will not get any error or exception).
這是一個靜態方法,也可以使用類名進行訪問。 (即,如果我們嘗試使用類對象進行訪問,則不會得到任何錯誤或異常)。
This method does not throw any exception at the time of synchronizing an ArrayList.
在同步ArrayList時,此方法不會引發任何異常。
Syntax:
句法:
public static List synchronizedList(List list);
Parameter(s):
參數:
list – represents the ArrayList to be binded in a synchronized list.
list –表示要綁定到同步列表中的ArrayList。
Return value:
返回值:
The return type of this method is List, it returns the synchronized view of the given list.
該方法的返回類型為List ,它返回給定列表的同步視圖。
Example:
例:
// Java program to demonstrate the example of
// synchronizing an ArrayList by using synchronizedList() method
import java.util.*;
public class SynchronizeArrayList {
public static void main(String[] args) {
// ArrayList Declaration
ArrayList al = new ArrayList();
// By using add() method to add few elements in
//ArrayList
al.add(10);
al.add(20);
al.add(30);
al.add(40);
al.add(50);
// Display ArrayList
System.out.print("Display ArrayList : " + " ");
System.out.println(al);
Collections.synchronizedList(al);
synchronized(al) {
Iterator itr = al.iterator();
System.out.println("Display synchronized ArrayList:");
while (itr.hasNext())
System.out.println(itr.next() + " ");
}
}
}
Output
輸出量
Display ArrayList : [10, 20, 30, 40, 50]
Display synchronized ArrayList:
10
20
30
40
50
2)使用CopyOnWriteArrayList同步ArrayList (2) Synchronizing ArrayList using CopyOnWriteArrayList)
CopyOnWriteArrayList is a synchronized thread-safe class.
CopyOnWriteArrayList是一個同步的線程安全類。
In the case of CopyOnWriteArrayList, more than one threads are allowed to work on.
對于CopyOnWriteArrayList,允許多個線程進行處理。
It works on different cloned copy for update operations.
它適用于不同的克隆副本以進行更新操作。
During one thread iterating CopyOnWriteArrayList object and at the same time other thread can modify because it works on the separate cloned copy.
在一個線程中迭代CopyOnWriteArrayList對象時,同時另一個線程可以修改,因為它可以在單獨的克隆副本上工作。
Example:
例:
// Java program to demonstrate the example of
// synchronizing an ArrayList by using CopyOnWriteArrayList
import java.util.*;
import java.util.concurrent.*;
public class SynchronizeArrayList {
public static void main(String[] args) {
// CopyOnWriteArrayList Declaration
CopyOnWriteArrayList < Integer > cowal = new CopyOnWriteArrayList < Integer > ();
// By using add() method to add few elements in
// CopyOnWriteArrayList
cowal.add(10);
cowal.add(20);
cowal.add(30);
cowal.add(40);
cowal.add(50);
// Display ArrayList
System.out.print("Display CopyOnWriteArrayList : " + " ");
System.out.println(cowal);
// Iterate ArrayList using iterator()
Iterator < Integer > itr = cowal.iterator();
System.out.println("Display synchronized ArrayList:");
while (itr.hasNext())
System.out.println(itr.next() + " ");
}
}
Output
輸出量
Display CopyOnWriteArrayList : [10, 20, 30, 40, 50]
Display synchronized ArrayList:
10
20
30
40
50
翻譯自: https://www.includehelp.com/java/synchronize-arraylist-in-java.aspx