What is the difference between ArrayList and vectors? What's the difference between HashMap and Hashtable? What's the difference between StringBuilder and StringBuffer? These are the basic questions that are common in the Java interview. In the face of this problem, the answer is: ArrayList is non-thread-safe, vector is thread-safe, HashMap is non-thread-safe, Hashtable is thread-safe, StringBuilder is non-thread-safe, StringBuffer is thread-safe. Because this is last night on the back of the "Java Plane Test book" above. At this point, if you continue to ask: what is thread safety? What is the difference between thread-safe and non-threading security? Under what circumstances are they used? Such a series of problems, a mouthful of old blood is sprayed out ...
How to choose between them
Non-thread-safe refers to a problem that can occur with multithreaded operations on the same object . Thread safety is not a problem with multithreaded operations on the same object .
Thread safety must use many synchronized keywords to synchronize control, so it inevitably leads to degraded performance.
So when using, if multiple threads are working on the same object, use a thread-safe vector, or use a more efficient ArrayList.
Non-thread safe! = is not secure
Someone in the process of using an incorrect point of view: My program is multi-threaded, can not use ArrayList to use the vector, so it is safe.
Non-thread-safe is not available in multithreaded environments. Notice what I've said above: multithreading operates on the same object. Note that it is the same object. A arraylist of new in the main thread and then multiple threads manipulate the same ArrayList object.
If it is a new ArrayList in each thread, and this ArrayList is only used in this thread, then it is certainly not a problem.
Thread-Safe implementation
Thread safety is implemented through thread synchronization control, which is the Synchronized keyword.
Java thread-Safe and non-threading security issues