Java multithreading (10) uses a thread-safe set and java Multithreading
Reprinted please indicate the source: http://blog.csdn.net/xingjiarong/article/details/48046751
In multithreading, if you want to modify a Data Structure concurrently, the data structure may be damaged. For example, a thread may want to insert an element into a hash. If an element is out of control when the link between buckets is adjusted, in this case, another thread is in a traversal table, and an exception or an endless loop may occur.
Locks can be used to protect the shared data structure, but it is easier to choose thread-safe implementation as an alternative.
I. Collection of old thread security
Any collection class can be changed to thread-safe by using the synchronous package:
List<E> synchArrayList = Collections.synchronizedList(new ArrayList<E>());Map<K,V> synchMap = Collections.synchronizedList(new HasMap<K,V>());
Results set methods are protected by locks to provide thread-safe access.
If you want to iterate the set when another thread may modify it, you need to block it.
Synchronized (synchHashMap) {Iterator <K> iter = synchHashMap. keySet (). iterator (); while (iter. hasNext () // traversal}
If you use a for each loop, you must use the same code because the loop uses an iterator. If another thread modifies the set during iteration, the iterator will become invalid and throw a ConcurrentModificationException. Therefore, concurrent modifications can be reliably detected.
2. Efficient image, set, and queue
The java. util. concurrent package provides efficient implementation of images, ordered sets, and queues: ConcurrentHashMap, ConcurrentSkipListMap, and concurrent1_queue. These sets use complex algorithms to minimize competition by allowing concurrent access to different parts of the data structure.
These sets return weak consistency iterators. This means that the iterator does not necessarily reflect all the modifications they have made after being constructed. However, they do not return the same value twice or throw the ConcurrentModificationException exception.
For example:
Import java. util. *; import java. util. concurrent. *;/** concurrent1_queue is a "thread-safe" queue, while the queue list is non-thread-safe. ** The following is an example of "multiple threads operate at the same time and traverse the queue" * (01) when the queue is a concurrent1_queue object, the program runs normally. * (02) when the queue is a queue list object, the program will generate a ConcurrentModificationException. ** @ Author skywang */public class Main {// TODO: When queue is an upload list object, an error occurs. // Private static Queue <String> queue = new Queue list <String> (); private static queue <String> Queue = new concurrent1_queue <String> (); public static void main (String [] args) {// start two threads simultaneously to operate the queue! New MyThread ("ta "). start (); new MyThread ("tb "). start ();} private static void printAll () {String value; Iterator iter = queue. iterator (); while (iter. hasNext () {value = (String) iter. next (); System. out. print (value + ",");} System. out. println ();} private static class MyThread extends Thread {MyThread (String name) {super (name) ;}@ Override public void run () {int I = 0; while (I ++ <6) {// "thread name "+"-"+" No. "String val = Thread. currentThread (). getName () + I; queue. add (val); // traverse the queue through" Iterator. PrintAll ();}}}}
No error is reported when concurrent1_queue is used.
An error occurs when using the sequence list:
Copyright Disclaimer: This article is the original author article, reprint please indicate the source, view the original article, please visit: http://blog.csdn.net/xingjiarong