Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the thread safety issues in the Collectio collection

Shulou Source: shulou.com Published: 2022-06-02 09:50:39 10月04日 Update

This article mainly explains "what are the thread safety problems in the Collectio collection". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the thread safety problems in the Collectio collection"?

1 List

Whether package com.shi.list;import java.util.List;import java.util.UUID;import java.util.concurrent.CopyOnWriteArrayList;/** * ArrayList rethreading is safe * @ author shiye * 1 new ArrayList () * result: a large number of exceptions are thrown * 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 reason: the add () method is not locked 3 solution 3.1 uses new Vector () Security issues can be solved but concurrency problems cannot be guaranteed. 3.2 use Collections.synchronizedList (new ArrayList ()) 3.3.Use new CopyOnWriteArrayList (); make a copy when writing and then operate * / 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 thread unsafety issues * @ author shiye * * 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 / * Thread unsafe problems in Map * @ author shiye * * problems in using 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} solution: 1 Collections.synchronizedMap (new HashMap ()); 2 use 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; jacks +) {map.put (j + ", (String) UUID.randomUUID () .toString () .subSequence (0,5));} System.out.println (map) System.out.println ();}) .start () Thank you for reading, the above is the content of "what are the thread safety issues in the Collectio collection". After the study of this article, I believe you have a deeper understanding of the thread safety problems in the Collectio collection, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Problems safety threads learning content solutions solutions reasons that is ideas situations articles methods aspects time more knowledge knowledge points articles results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker OPPO Reno vpn macOS