Understanding of thread-safe

  1. The method of variable does not exist non-thread-safety issue, always thread-safe, because the internal method variables are private properties caused.
  2. added to the lock synchronized method, the object is locked. Effect with synchronized (this)
  3. synchronized is applied to the static method, the lock is a class. Effect with synchronized (class.Class).
  4. Multiple objects multiple locks. Two threads are accessing the synchronization method of the same name in two different instances of the same class, the effect is running in an asynchronous way. Keyword synchronized lock object locks are acquired, rather than as a piece of code or lock method, which thread to execute the method with the synchronized keyword, which thread which holds that the method belongs to the object lock Lock, then the other thread only It was able to wait state if multiple threads access the same object. However, if multiple threads access multiple objects, the JVM creates multiple locks. Synchronization word is synchronized, asynchronous word is asynchronized.
  5. Plus an unlocked mainly depends on: the implementation of the method, there is no data in the method may be modified in other places (other threads). For example a class variable, read and write the database.
Published 10 original articles · won praise 0 · Views 133

Guess you like

Origin blog.csdn.net/m178643/article/details/102491824