Thread-Safe Collection objects in Java

Source: Internet
Author: User

Vector and ArrayList

Vectors are thread-safe, but in most cases do not use vectors, because thread safety requires greater overhead.


Hashtable and HashMap

The methods in Hashtable are synchronous, and the methods in HashMap are not synchronized by default. In the context of multi-threaded concurrency, you can use Hashtable directly, but to use hashmap, you need to increase the synchronization process.


StringBuilder and StringBuffer

StringBuilder and StringBuffer are identical in that the former is multithreaded and the latter is single-threaded.


Stack inherits from Vector is also thread-safe.


A thread-safe collection can be created by java.util.Collections, such as: Connections.synchronizedset (set<t>); Connections.synchronizedlist (list<t>); Connections.synchronizedmap (Map<k, v>), and so on, its simple principle is that each method adds synchronized to ensure thread safety.



may not be added after all.

Thread-Safe Collection objects in Java

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.