in the collection API, the originally designed vectors and Hashtable are multithreaded secure. For example, for vectors, the methods used to add and delete elements are synchronized. If only one thread interacts with an instance of a vector, it is wasteful to ask for and release object locks, and it is possible to create deadlocks if misuse is synchronized unnecessarily. Therefore, none of the methods for changing the contents of a collection are synchronized. Collections are inherently non-multithreaded security, and when multiple threads interact with the collection, additional measures must be taken to make multithreading secure.
There are several static methods in the collections class that can obtain a collection that is encapsulated by a synchronous method to encapsulate a non-synchronous collection:
public static Collection synchronizedcollention (Collection c)
public static list synchronizedlist (list L)
public static map Synchronizedmap (map m)
public static set Synchronizedset (set S)
public static SortedMap Synchronizedsortedmap (SortedMap sm)
public static SortedSet Synchronizedsortedset (SortedSet ss)
These methods basically return a new class with a synchronous collection method version. For example, to create a list that is multithreaded and secure and supported by ArrayList, you can use the following code:
List List = Collection.synchronizedlist (new ArrayList ());
Note that the ArrayList instance is immediately encapsulated and there is no direct reference to the unsynchronized ArrayList (that is, to encapsulate the anonymous instance directly). This is one of the safest ways. If another thread is to refer directly to the ArrayList instance, it can perform a non-synchronous modification.
The following is an example of a safe traversal of a collection element in a multi-threaded thread. We use iterator to scan the elements in list one by one, and in a multithreaded environment, when traversing elements in the current collection, you generally want to prevent other threads from adding or removing elements. Security traversal is implemented as follows:
[Java]View Plaincopy
- Import java.util.*;
-   
- public class safecollectioniteration extends object {
- public static void main (String[] args) {
- //for security purposes, only one reference to the synchronization list is used, which ensures that all access to the   
- //collection must be synchronized, here is a list   
- list wordlist = Collections.synchronizedlist (new arraylist ());
-   
- //wordlist is a synchronous method that gets the object lock of the Wordlist instance   
- wordlist.add (" iterators "
- wordlist.add (" require "
- wordlist.add (" special "
- wordlist.add (" handling "
-   
- //gets the object lock of the Wordlist instance,   
- //iterations, blocks other threads from calling add or remove methods to modify the element   
- synchronized ( wordList ) {
- iterator iter = wordlist.iterator ();
- while ( iter.hasnext () ) {
- String s = (String) iter.next ();
- system.out.println (" found string: "  + S +  ", length= " + s.length ());
- }
- }
- }
- }
It is important to note that in the Java language, most of the thread-safe classes are relatively thread-safe, which guarantees thread-safe operation of the object individually, and we do not need additional safeguards when calling, but for some specific sequential calls, It may be necessary to use an additional synchronization method at the caller's side to ensure the correctness of the call. Examples include vectors, HashTable, Collections's Synchronizedxxxx () method wrapper collections, and so on.
"Java Concurrency Programming" Eight: Secure use of collection APIs (with code) in multithreaded environments