background

  1. Learn the simple use of ThreadLocal.
  2. Learning to understand a thing begins with its definition and concept.
  3. Learn to ask why? Why does ThreadLocal exist? What problem does its existence solve?
  4. Learn to compare techniques, or at least develop awareness of them.

The core content

  • Simple use of ThreadLocal
  1. The example code

2. The test code 3. The test results 4. Code result analysis

Both threads share the same ThreadLocalDemo instance. Num.set (num.get() + 1) will be executed when one of the threads, any thread, gets the next value. The other thread, if affected, should have a value of 2, but actually prints 1.

As a result, threads operating on the same ThreadLocalDemo instance are safe and have no effect on each other.

  • What is a ThreadLocal?
  1. Space for time.
  2. Thread-independent private space.
  3. A new way to solve concurrency problems.
  • Why ThreadLocal? It solves the problem of slow synchronized performance

    In general, when solving multithreading problems, we all lock. Locking is actually synchronized. However, the operation of synchronization is a time-consuming operation. Why? When one thread acquires the lock, all other threads must wait until the thread completes and releases the lock. So this process is very time consuming. So synchronized is time-consuming.

    However, some would argue that not necessarily. Sometimes synchronized is used for higher performance. How is that possible? None of the scenarios are possible.

  • ThreadLocal vs. Synchronized

    project space Execution speed
    ThreadLocal big fast
    synchronized small slow
  • Analyzed from the above table

    It can be inferred from the table above. If you have a lot of memory, you can use ThreadLocal instead of synchronized. Synchronized can be used if memory space is low and performance requirements are not particularly high.

  • Usage scenarios

  1. Demand: Are space resources scarce? Are there very high requirements for speed of execution?

  2. Multiple threads share a variable that needs to be thread exclusive, that is, changes made by one thread cannot affect other threads.

  3. Analyze such a business scenario. A variable in a multi-threaded environment, whether the thread is exclusive or shared. If exclusive in a multi-threaded environment, use ThreadLocal. In the case of thread sharing, one thread has modified the content, and subsequent threads can see and modify it again based on the new value, either using synchronized or some of the atomic manipulation classes in the synchronized package, which also provide data manipulation on reference types.

  • Matters needing attention
  1. When a thread terminates, local variables to the thread are automatically garbage collected, so explicitly calling this method to remove local variables from the thread is not required, but it can speed up memory collection.

  2. JDK5.0 has been upgraded. ThreadLocal becomes ThreadLocal. void set(T value), T get(T value),T initialValue()

  • Simple to understand
  1. If you have a global variable, such as an instance, and there are multiple threads, each thread will make changes to the contents of the instance, but the changes are independent of each thread, meaning that changes made by one thread cannot affect other threads.

  2. Each thread operates on the same instance object, but the attributes in the instance object are private to each thread and do not affect each other. The internal ThreadLocalMap class of the ThreadLocal instance stores the current thread as the key and initialValue as the corresponding value. In use, the current thread retrieves the value, processes the business, and puts the value back.

summary

  1. The use of ThreadLocal is not important and can be queried immediately if needed. Just as there is no need to memorize the JDK API or any other framework API, when using it, just query it.

  2. What you can actually learn from ThreadLocal, or what ThreadLocal can teach you, is the value.

  3. Learn to compare the advantages and disadvantages of ThreadLocal and synchronized.