如何在Java中同步ArrayList?

同步ArrayList (Synchronizing ArrayList)

In java, there are two ways to synchronize ArrayList,

在Java中,有两种同步ArrayList的方法,

  1. With the help of synchronizedList() method

    借助syncedList()方法

  2. 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值