温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Collectio集合中的线程安全问题有哪些

发布时间:2021-06-24 09:53:04 来源:亿速云 阅读:123 作者:chen 栏目:大数据

这篇文章主要讲解了“Collectio集合中的线程安全问题有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Collectio集合中的线程安全问题有哪些”吧!

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<>(); 
 * 结果:抛出大量的异常  
 * 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<>() ,安全问题可以解决 但是并发性问题无法得到保障
		3.2 使用 Collections.synchronizedList(new ArrayList<>());
		3.3 使用 new CopyOnWriteArrayList(); 写的时候复制一份 再去操作
 */
public class TestArrayList1 {

	public static void main(String[] args) {
		//1 new ArrayList<>();
		//2 new Vector<>();
		//3 Collections.synchronizedList(new ArrayList<>());
		List<String> 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();
		}
	}

}

Collectio集合中的线程安全问题有哪些

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 
 * 会导致  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<String> 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();
		}
	}

}

Collectio集合中的线程安全问题有哪些

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 出现的问题
 * {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<String,Object> 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集合中的线程安全问题有哪些”的内容了,经过本文的学习后,相信大家对Collectio集合中的线程安全问题有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI