"Developer Log" Thread-safe

Source: Internet
Author: User
Tags benchmark hash

In the Web to use multi-threading, multi-threaded read and write a collection, will involve the Thread-safe problem, if collection use synchronize in the operation, then this is thread safety, but this is to pay the performance cost, You can also not apply thread-safe, but you need to consider whether or not to add synchronization to your needs.

There is a detailed parsing of what is thread safety in http://www.asjava.com/core-java/thread-safe-hash-map-in-java-and-their-performance-benchmark/, Additions and deletions entry is related to thread safety, only the value corresponding to the change key is not considered (this may be related to atomic operations, do not worry).

What does the thread safe Map means? If Multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must Be synchronized externally to avoid an inconsistent view of the contents. (A structural Modification is any operation this adds or deletes one or more mappings; merely changing the value associate D with a key, an instance already contains are not a structural modification.)

For List,vector is thread safe, but ArrayList is not. In Javadoc, this describes the vector:

As of the Java 2 platform v1.2, this class is retrofitted to implement Thelist interface, making it a member of Thejava C Ollections Framework. Unlike the new collection Implementations,vector is synchronized. If a Thread-safe implementation is not needed, it's recommended to usearraylist on place Ofvector.

For map,hashtable is thread-safe, but HashMap is not. But using ancient Hashtable sometimes does not perform well, and Hashtable does not allow Nulls as a value, and HashMap allows it. We can do this in two different ways. For example, an alternative

map<string, integer> threadsafemap = new hashtable<string, integer> ();

Mode 1: Using Collections.synchronizedmap

Map<string,integer>threadsafemap = Collections.synchronizedmap (New hashmap<string, Integer> ());

Mode 2: Using the Concurrenthashmap introduced in Java 1.5

map<string,integer> Threadsafemap = new concurrenthashmap<string, integer> ();

According to the test data of http://www.asjava.com/core-java/thread-safe-hash-map-in-java-and-their-performance-benchmark/, Concurrenthashmap is more efficient. Therefore, when we need to use Thread-safe, we should use Concurrenthashmapas much as we can.

RELATED Links: Development log

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.