📜  Java中的集合 synchronizedCollection() 方法及示例

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

Java中的集合 synchronizedCollection() 方法及示例

Java.util.Collections类的synchronizedCollection()方法用于返回由指定集合支持的同步(线程安全)集合。为了保证串行访问,对支持集合的所有访问都通过返回的集合完成是至关重要的。
句法:

public static  Collection
  synchronizedCollection(Collection c)

参数:此方法将集合 c作为参数“包装”在同步集合中。
返回值:此方法返回指定集合的同步视图
以下是说明synchronizedCollection()方法的示例
示例 1:

Java
// Java program to demonstrate synchronizedCollection()
// method for String Value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of List
            List vector = new ArrayList();
 
            // populate the vector
            vector.add("A");
            vector.add("B");
            vector.add("C");
            vector.add("D");
            vector.add("E");
 
            // printing the Collection
            System.out.println("Collection : " + vector);
 
            // getting the synchronized view of Collection
            Collection c = Collections
                                       .synchronizedCollection(vector);
 
            // printing the Collection
            System.out.println("Synchronized view"
                               + " of collection : " + c);
        }
 
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate synchronizedCollection()
// method for Integer Value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of List
            List vector = new ArrayList();
 
            // populate the vector
            vector.add(20);
            vector.add(30);
            vector.add(40);
            vector.add(50);
            vector.add(60);
 
            // printing the Collection
            System.out.println("Collection : " + vector);
 
            // getting the synchronized view of Collection
            Collection c = Collections
                                        .synchronizedCollection(vector);
 
            // printing the Collection
            System.out.println("Synchronized view is : " + c);
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出
Collection : [A, B, C, D, E]
Synchronized view of collection : [A, B, C, D, E]

示例 2:

Java

// Java program to demonstrate synchronizedCollection()
// method for Integer Value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of List
            List vector = new ArrayList();
 
            // populate the vector
            vector.add(20);
            vector.add(30);
            vector.add(40);
            vector.add(50);
            vector.add(60);
 
            // printing the Collection
            System.out.println("Collection : " + vector);
 
            // getting the synchronized view of Collection
            Collection c = Collections
                                        .synchronizedCollection(vector);
 
            // printing the Collection
            System.out.println("Synchronized view is : " + c);
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Collection : [20, 30, 40, 50, 60]
Synchronized view is : [20, 30, 40, 50, 60]