Thread safety for various Java collections

Source: Internet
Author: User
thread safety for various Java collectionsThread Safety

The first thing to understand is how the thread works, the JVM has a main memory, and each thread has its own working
Memory, when a thread operates on a variable, it has to be in its own working
Create a copy inside the memory, then write to main after the operation is done
Memory When multiple threads operate the same variable at the same time, unpredictable results can occur. According to the above explanation, it is easy to come up with the corresponding scenario.
The key to using synchronized is to create a monitor that can be modified by the variable or other object you think is appropriate, such as method, and then thread-safe by locking the monitor. After each thread obtains the lock, it performs the
Load to workingmemory-> use&assign-> store to Mainmemory
Process before releasing it to get the lock. This enables the so-called thread safety.

What is thread safety? How does thread safety work (principle)? Thread safety means that multithreading accesses the same code without creating an indeterminate result. Writing thread-Safe code is low reliance on thread synchronization. Java related collection Vector, ArrayList, LinkedList

    Vectors and ArrayList are very similar in use, and can be used to represent a set of variable sets of object applications and to randomly access elements of them.  
vector methods are synchronous (Synchronized), Thread-safe (Thread-safe), and ArrayList's approach is not that because the synchronization of threads necessarily affects performance, ArrayList performance is better than vector.  

    ArrayList and LinkedList differences  
for processing a column of data items, Java provides two classes ArrayList and linkedlist,arraylist internal implementations are based on internal array object[], so conceptually , it's more like an array, but the internal implementation of LinkedList is based on a set of connected records, so it's more like a list structure, so there's a big difference in performance.  
from the analysis above, you have to insert data in front or in the middle of the ArrayList, you have to move all the data back accordingly, which will take more time, so when your operation is to add data after a column of data rather than in front or in the middle, And when you need to randomly access the elements in it, using ArrayList to provide better performance  
and access to an element in the list, you have to search from one end of the list along the direction of the link, until you find the element you want, so You should use LinkedList when you are adding or deleting data in front or in the middle of a column of data, and accessing the elements in order.  
if in the programming, 1, 22 kinds of situations alternately appear, at this time, you may consider uses the list such universal interface, but does not care about the concrete implementation, in the concrete situation, its performance is guaranteed by the concrete implementation.    
1 2 3 4 5 6 7 8 9 Hashtable,hashmap,hashset
Hashtable and HashMap adopt the same storage mechanism, the implementation of the two basically consistent, the difference is: 1, HashMap is not thread-safe,

Hashtable is thread-safe, the internal method is basically synchronized.

2), Hashtable does not allow the existence of null values.

when the Put method is invoked in Hashtable, if the key is null, the NullPointerException is thrown directly. Other subtle differences, such as initializing the size of the entry array, and so on, but the basic idea is the same as HashMap.

HashSet:
1, HashSet based on HASHMAP implementation, no capacity constraints.
2, HashSet is not thread-safe. 
3, HashSet does not guarantee orderly. 

HashMap:
1, the HASHMAP uses the array way to store the Key,value composition entry object, does not have the capacity limit.
2, HashMap based on the key hash to find the entry object to the location of the array, for the hash conflict using a linked list of methods to solve.
3. HashMap may want to enlarge the capacity of the array when inserting elements, it is necessary to recalculate the hash when enlarging the capacity, and copy the object into the new array.
4, HashMap is not thread-safe.
5, HashMap traversal use is iterator 

HashTable
1, HashTable is thread-safe.
2, Hashtable, whether the key, or value is not allowed to null.
1 2 3 4 5 6 7 8 9 (TREESET,TREEMAP) (+)
TreeSet:
 1, TreeSet based on TREEMAP implementation, support sorting.
 2, TreeSet is not thread-safe.

    from the descriptions of HashSet and TreeSet, TreeSet and HashSet are also completely based on the map and do not support getting (int) to get the element at the specified location (which requires traversal fetching). In addition, TreeSet also provides some sort of support. Examples include incoming comparator implementations, Descendingset, and Descendingiterator.

TreeMap: 
1, TreeMap is a typical map based on the red-black tree implementation, so it requires that there must be a key comparison method, either in the comparator implementation, or the key object to implement the comparable interface.
2, TreeMap is not thread-safe.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.