What is thread safety

What is thread safety? This is actually similar to the concept of database consistency, that is, multi-threaded write operations on the same global variable or static variable at the same time, there will not be inconsistent data or unexpected conditions. Implement thread safety by locking mechanism.

Second, when to need thread safety multithreading.

Third, why there are classes that are not thread-safe look up some class instructions, either. NET or JAVA, it is sometimes said that this class is not thread-safe, meaning that it is not thread-safe in multi-threaded operations. For example, HashMap. Why? In order to ensure thread safety, it is necessary to use the lock, which will be a loss of performance. Multithreading is not necessary in all cases; So there are thread-unsafe classes.