十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要講解了“Collectio集合中的線程安全問題有哪些”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Collectio集合中的線程安全問題有哪些”吧!
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供塔城網(wǎng)站建設(shè)、塔城做網(wǎng)站、塔城網(wǎng)站設(shè)計、塔城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、塔城企業(yè)網(wǎng)站模板建站服務(wù),10年塔城做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
1 List
package com.shi.list;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* ArrayList再線程安全不安全方面的問題
* @author shiye
* 1 new ArrayList<>();
* 結(jié)果:拋出大量的異常
* java.util.ConcurrentModificationException
at java.util.ArrayList.forEach(Unknown Source)
at com.shi.list.TestArrayList1.lambda$0(TestArrayList1.java:20)
at java.lang.Thread.run(Unknown Source)
2 原因:add()方法沒有加鎖
3 解決方案
3.1 使用 new Vector<>() ,安全問題可以解決 但是并發(fā)性問題無法得到保障
3.2 使用 Collections.synchronizedList(new ArrayList<>());
3.3 使用 new CopyOnWriteArrayList(); 寫的時候復(fù)制一份 再去操作
*/
public class TestArrayList1 {
public static void main(String[] args) {
//1 new ArrayList<>();
//2 new Vector<>();
//3 Collections.synchronizedList(new ArrayList<>());
List list = new CopyOnWriteArrayList();
for (int i = 0; i < 30; i++) {
new Thread( () -> {
list.add((String) UUID.randomUUID().toString().subSequence(0, 5));
System.out.println(list);
System.out.println();
}).start();
}
}
} 
2 Set
package com.shi.list;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* Set的線程不安全問題
* @author shiye
*
* 如果直接使用 HashSet
* 會導(dǎo)致 Exception in thread "Thread-26" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(Unknown Source)
*/
public class TestArraySet1 {
public static void main(String[] args) {
Set set = new CopyOnWriteArraySet(); //Collections.synchronizedSet(new HashSet()); //new HashSet<>();
for (int i = 0; i < 30; i++) {
new Thread( () -> {
set.add((String) UUID.randomUUID().toString().subSequence(0, 5));
System.out.println(set);
System.out.println();
}).start();
}
}
} 
3 Map
package com.shi.list;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
/**
* Map 中的線程不安全問題
* @author shiye
*
* 使用 HashMap 出現(xiàn)的問題
* {0=abf8f, 1=6d6e8, 2=945c7, 3=bd78e, 4=76208}
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(Unknown Source)
{0=a0f65, 1=dab37, 2=15df6}
解決方案:
1 Collections.synchronizedMap(new HashMap<>());
2 使用 new ConcurrentHashMap(); //java.util.concurrent.ConcurrentHashMap
*
*/
public class TestMap {
public static void main(String[] args) {
Map map = new ConcurrentHashMap();//Collections.synchronizedMap(new HashMap<>());//new HashMap<>();
for (int i = 0; i < 30; i++) {
new Thread( () -> {
for (int j = 0; j < 100; j++) {
map.put(j + "" ,(String) UUID.randomUUID().toString().subSequence(0, 5));
}
System.out.println(map);
System.out.println();
}).start();
}
}
} 感謝各位的閱讀,以上就是“Collectio集合中的線程安全問題有哪些”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Collectio集合中的線程安全問題有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!