Multithreading - synchronized and ThreadLocal What is the difference?

synchronized using the lock mechanism, the variable code block at a time, or that only one thread to access. The ThreadLocal each thread provides a copy of the variable so that each thread at a time to visit is not the same object, thus isolating the multiple threads of data for data sharing. Synchronized but just the opposite, it is used at the time of communication can be obtained between a plurality of threads to share data. which is

  • Synchronized for data sharing between threads
  • ThreadLocal are used for data isolation between threads

It does not replace ThreadLocal synchronized, Synchronized wider range function (synchronization).

Guess you like

Origin www.cnblogs.com/frankcui/p/12446101.html