ThreadLocal usage scenarios, principles

ThreadLocal
1. Under the first ThreadLocal can not resolve a number of threads share data, he is a multi-threaded shared data isolation between good helper
2. ThreadLocal local threads share data
3. He is space for time

the synchronized
1. solve problems across multiple threads share data security
2. He is the time for space programs inefficiently (less applicable when the amount of concurrency)

Note:
ThreadLocal and Synchonized are used to solve multi-threaded concurrent access.
But with the synchronized ThreadLocal are essentially different.
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.
Synchronized for data sharing between threads, and ThreadLocal data isolation is used between threads.

Note: This article said: https://blog.csdn.net/liuhaiabc/article/details/78077529

Guess you like

Origin www.cnblogs.com/duende99/p/12079183.html