如何在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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377754.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377754.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377754.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

十一、決策樹和隨機森林

這門課和另一門課內容都差不多&#xff0c;可以參考七、決策樹算法和集成算法該篇博文。 一、決策樹相關概念 邏輯回歸本質 邏輯回歸&#xff1a;線性有監督分類模型。常用求解二分類問題&#xff0c;要么是A類別要么是B類別&#xff0c;一般會以0.5作為劃分閾值&#xff0c…

【C++grammar】繼承與構造

目錄1.繼承1、Inheritance (繼承)2、避免一個類被繼承&#xff08; C11 &#xff09;3、繼承實例4、完整代碼5、繼承的優缺點是什么?2.繼承中的構造函數1、 派生類繼承的成員2、調用基類構造函數3.繼承中的默認構造函數1、基類的無參構造函數2、由編譯器自動生成的基類構造函數…

C語言預處理

所謂預處理是指在進行編譯的第一遍掃描(詞法掃描和語法分析)之前所作的工作。預處理是&#xff23;語言的一個重要功能&#xff0c; 它由預處理程序負責完成。當對一個源文件進行編譯時&#xff0c; 系統將自動引用預處理程序對源程序中的預處理部分作處理&#xff0c; 處理完畢…

(轉)將cocos2dx項目從VS移植到Eclipse

本文轉自:http://www.cnblogs.com/Z-XML/p/3349518.html 引言&#xff1a;我們使用cocos2d-x引擎制作了一款飛行射擊游戲&#xff0c;其中創新性地融入了手勢識別功能。但是我們在移植過程中遇到了很多的問題&#xff0c;同時也發現網上的資料少而不全。所以在項目行將結束的時…

十二、聚類算法——相似度測量

兩套學習資料都類似&#xff0c;可參考聚類算法實戰 一、聚類 聚類&#xff1a;物以類聚&#xff0c;人以群分&#xff0c;是無監督學習中的一種。 沒有y&#xff0c;只有x&#xff0c;把不同的x根據相似度自動的聚成好多堆兒 本質上&#xff0c;N個樣本&#xff0c;映射到K個…

操作系統磁盤調度_磁盤調度| 操作系統

操作系統磁盤調度磁盤調度 (Disk Scheduling) One of the major duties of the operating is that, to use the hardware orderly and accurately. For disk drives, it has a duty of having a fast access time and disk bandwidth. Generally, bandwidth is the total numbe…

leetcode 344. 反轉字符串 541. 反轉字符串 II 雙指針解

目錄leetcode 344.反轉字符串1、題目2、思考leetcode 541. 反轉字符串 II1、題目2、思考leetcode 344.反轉字符串 1、題目 2、思考 典型的雙指針解法&#xff1a; 一個從前往后&#xff0c;一個從后往前&#xff0c;指針對應的交換即可。 class Solution { public:void reve…

關于銀聯在線支付和短彩信接口的開發——總結

9月份開始做用二維碼做閉環的一個在線訂購景區門票的項目&#xff0c;其中這樣做是很好的&#xff0c;用二維碼連接了線上與線下的交易和兌券。銀聯在線支付接口&#xff08;asp.net cs&#xff09;做的很好&#xff0c;方便調用開發。就是處理回值的時候得找個更好的方法才能顯…

十三、聚類算法

六、聚類算法實戰 一、聚類 聚類是一種無監督的機器學習任務&#xff0c;可以自動將數據劃分為類cluster&#xff0c;因此聚類分組不需要提前被告知所劃分的組應該是什么樣子的。因為我們甚至可能都不知道我們在尋找什么&#xff0c;所以聚類是用于知識發現而不是預測。 聚類…

pl/sql中的賦值運算符_如何在SQL中使用AND / OR運算符?

pl/sql中的賦值運算符Basically, AND / OR operator is used to retrieving the record from the database. If we give more than one conditions by using AND Operator, then it retrieves the data from the database when both the conditions are true. And if we use OR…

【C++grammar】名字隱藏與重定義

目錄1、繼承中的名字隱藏1.基類同名函數被隱藏的現象描述2.問題理解3.避免現象2、重定義1.現象描述2.重定義與重載的區別3.能否使用 using 將基類成員引入到派生類定義中1、繼承中的名字隱藏 1.基類同名函數被隱藏的現象描述 在學習變量作用域的時候知道&#xff0c;全局變量…

javascript 核心概念(1)-數據類型

語法 &#xff08;1&#xff09;到現在為止&#xff0c;大多數瀏覽器也還是支持到ECMAScript 第三版的標準。 核心概念就是一個語言的基本工作原理&#xff0c;涉及語法&#xff0c;操作符&#xff0c;數據類型。 &#xff08;2&#xff09;javascript的一切--變量&#xff0c;…

注解的力量 -----Spring 2.5 JPA hibernate 使用方法的點滴整理(五):使用@Component 來簡化bean的配置...

雖然我們可以通過 Autowired 在 Bean 類中使用自動注入功能&#xff0c;但是 Bean 還是在 applicatonContext.xml 文件中通過 <bean> 進行定義 —— 在前面的例子中&#xff0c;我們還是在配置文件中定義 Bean&#xff0c;通過 Autowired為 Bean 的成員變量、方法形參或構…

c語言條件語句示例_PHP中的條件語句和示例

c語言條件語句示例PHP條件語句 (PHP Conditional Statements) While coding, you may get to a point where your results can only be gotten when a condition is valid. We make use of conditional statements. Conditional statements are statements that can only be ex…

十四、聚類實戰——圖片壓縮

對同一像素點值的像素點歸為一類&#xff0c;通過平均值進行取代&#xff0c;從而將圖像進行壓縮并且保證圖像盡可能不失真&#xff0c;關鍵信息仍保留。 from PIL import Image import numpy as np from sklearn.cluster import KMeans import matplotlib import matplotlib.…

步驟菜單使用css3實現

代碼庫&#xff1a;http://thecodeplayer.com/walkthrough/css3-breadcrumb-navigation 有興趣的可以看一下&#xff0c;看完絕對讓你大飽眼福。首先截圖&#xff0c;看效果看著很酷吧&#xff0c;其實實現起來也不是很難&#xff0c;里邊需要用的技術有:box-shadow,計數器&…

【嵌入式系統】STM32串口通信的四種方法(基于RTOS)

目錄1、串行通信的基本參數2、輪詢方式代碼效果3、中斷方式代碼效果4、中斷加上時間戳方式代碼及效果5、DMA空閑中斷方式接收數據1、串行通信的基本參數 串行端口的通信方式是將字節拆分成一個接一個的位再傳輸出去&#xff0c;接收方再將此一個一個的位組合成原來的字符&…

大數據 java 代碼示例_Java變量類型與示例

大數據 java 代碼示例Java變量 (Java variables) Variables are the user-defined names of the memory blocks, and their values can be changed at any time during program execution. They play an important role in a class/program as they help in to store, retrieve…

畢業設計

位置跟蹤系統工作原理&#xff08;博聞網&#xff09; http://science.bowenwang.com.cn/location-tracking.htm Azuma是這樣定義增強現實的 :虛實結合 ,實時交互 ,三維注冊 環境搭建&#xff1a; http://cvchina.net/thread-173-1-1.html http://blog.csdn.net/jdh99/article/…

十五、聚類的評估

一、Given Label 均一性homogeneity&#xff1a;一個簇中只包含一個類別樣本&#xff0c;Precision 完整性completeness&#xff1a;同類別樣本被歸到同一個簇中&#xff0c;Recall 將均一性h和完整性c進行結合(二者加權平均)得到V-Measure&#xff0c;&#xff0c;β為權重 …