前言:
现时朋友们对“java集合类线程安全吗”大概比较关切,兄弟们都需要分析一些“java集合类线程安全吗”的相关内容。那么小编同时在网摘上汇集了一些关于“java集合类线程安全吗””的相关知识,希望兄弟们能喜欢,同学们快快来了解一下吧!前言:
记得很早之前,一面试的哥们跟我说,让我举个例子,用代码的方式证明线程不安全,然后如何解决线程不安全的问题,一时之间还真说不上来,后来我一直记得,今天整理了整理。
案例1:
/** * 集合类线程不安全(ArrayList),案例 */public class ThreadSafeCase02Demo01 { public static void main(String[] args) { List<String> arrayList = new ArrayList<String>(); for (int i = 0; i < 20; i++) { new Thread(() -> { arrayList.add(new Random().nextInt() + ""); System.out.println(arrayList); }, "name:" + i).start(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // 现象: java.util.ConcurrentModificationException // 在多线程读写情况下会抛出并发读写异常(ConcurrentModificationException): }}
解决方案:
/** * 集合类线程不安全(ArrayList),解决方案 */public class ThreadSafeCase02Demo01Solution01 { public static void main(String[] args) { List<String> arrayList = new CopyOnWriteArrayList<String>(); for (int i = 0; i < 30; i++) { new Thread(() -> { arrayList.add(new Random().nextInt() + ""); System.out.println(arrayList); }, "name:" + i).start(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // List线程安全CopyOnWriteArrayList // set线程安全CopyOnwriteHashSet // map线程安全ConcurrentHashMap }}案例2:
/** * 集合类线程不安全(HashSet),案例 */public class ThreadSafeCase02Demo02 { public static void main(String[] args) { Set<String> set = new HashSet<String>(); for (int i = 0; i < 20; i++) { new Thread(() -> { set.add(new Random().nextInt() + ""); System.out.println(set); }, "name:" + i).start(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // 现象: java.util.ConcurrentModificationException // 在多线程读写情况下会抛出并发读写异常(ConcurrentModificationException): }}
解决方案:
import java.util.Random;import java.util.Set;import java.util.concurrent.CopyOnWriteArraySet;/** * 集合类线程不安全(HashSet),解决方案 */public class ThreadSafeCase02Demo02Solution01 { public static void main(String[] args) { Set<String> set = new CopyOnWriteArraySet<String>(); for (int i = 0; i < 30; i++) { new Thread(() -> { set.add(new Random().nextInt() + ""); System.out.println(set); }, "name:" + i).start(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // List线程安全CopyOnWriteArrayList // set线程安全CopyOnWriteArraySet // map线程安全ConcurrentHashMap }}案例3:
import java.util.HashMap;import java.util.Map;import java.util.Random;/** * 集合类线程不安全(HashMap),案例 */public class ThreadSafeCase02Demo03 { public static void main(String[] args) { Map<String, String> hashMap = new HashMap<String, String>(); for (int i = 0; i < 20; i++) { new Thread(() -> { hashMap.put(new Random().nextInt() + "", new Random().nextInt() + ""); System.out.println(hashMap); }, "name:" + i).start(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // 现象: java.util.ConcurrentModificationException // 在多线程读写情况下会抛出并发读写异常(ConcurrentModificationException): }}
解决方案:
import java.util.Map;import java.util.Random;import java.util.concurrent.ConcurrentHashMap;/** * 集合类线程不安全(HashMap),解决方案 */public class ThreadSafeCase02Demo03Solution01 { public static void main(String[] args) { Map<String, String> hashMap = new ConcurrentHashMap<String, String>(); for (int i = 0; i < 20; i++) { new Thread(() -> { hashMap.put(new Random().nextInt() + "", new Random().nextInt() + ""); System.out.println(hashMap); }, "name:" + i).start(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // List线程安全CopyOnWriteArrayList // set线程安全CopyOnWriteArraySet // map线程安全ConcurrentHashMap }}
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #java集合类线程安全吗