30 Which collection classes are thread safe?

Which collection classes are thread-safe?

answer:

  • Vector: There is more synchronization mechanism (thread safety) than Arraylist.

  • Hashtable: It is more thread safe than Hashmap.

  • Stack: Stack, also thread-safe, inherited from Vector.

  • ConcurrentHashMap: is an efficient but thread-safe collection

expand:

  • After JDK1.5, with the emergence of Java.util.concurrent concurrent packages, for example, HashMap also has a corresponding thread safety class ConcurrentHashMap.

  • As early as version 1.1 of jdk, all collections are thread-safe. However, there are some thread-unsafe collections in version 1.2 and later. Why are there some thread-unsafe collections in the version upgrade? Because thread-unsafe collections are generally much more efficient than thread-safe collections. With the development of services, especially in web applications, in order to improve the user experience and reduce user waiting time, page response speed (that is, efficiency) is a priority. Moreover, after locking a thread-unsafe collection, it can also achieve a safe effect (but the efficiency will be low, because there will be lock acquisition and waiting). In fact, in the jdk source code, the same effect collection thread is safer than the thread unsafe, there is one more synchronization mechanism, but the efficiency is more than a little lower, because the efficiency is low, it is not recommended.

Reference blog post: https://blog.csdn.net/qq_32534441/article/details/94737311

Guess you like

Origin www.cnblogs.com/ynzj123/p/12735714.html