In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.