ThreadLocal in Java

Source: Internet
Author: User

First, ThreadLocal is not used to solve multi-threaded access problems with shared objects, and in general, objects in the thread through Threadlocal.set () are objects that the thread itself uses, and other threads do not need access or access. Different objects are accessed in each thread.

in addition, It is said that threadlocal enables each thread to maintain its own independent object, not by Threadlocal.set (), but by creating an object through the operation of the new object in each thread, creating a copy or a copy of each thread that is not an object. The reference to this newly created object is saved to a map of each thread by Threadlocal.set (), each thread has a map that executes Threadlocal.get (), and each thread pulls the object from its own map and So the objects in their own thread are taken out, and the threadlocal instance is used as the key to the map.
If Threadlocal.set () goes in the same object that multiple threads share, then the Threadlocal.get () of multiple threads gets the shared object itself, or there is a concurrent access problem.



Summary: Threadlocal.set () in the object will exist in the current thread in a map, and the current ThreadLocal itself as a key, can only say that the map can guarantee the unique, the map of the object if it is stored through new, The object is unique to the thread if it is not persisted by another thread reference, otherwise there may be a multithreaded concurrency problem because it is a reference type


Test code:

  
 
  1. public static  ThreadLocal<String> threadLocal = new ThreadLocal<String>(); 
  2.   
  3.  public static void main(String[] args) { 
  4.    
  5.   object []  objects = new object []{ 1 2 Span class= "pun" "}; &NBSP;
  6.   System.out.println(Arrays.toString(objects)); 
  7. //   
  8. // for (int i = 0; i< 10 ;i++) { 
  9. //    
  10. // final int j= i ; 
  11. // new Thread(new Runnable() { 
  12. //     
  13. // @Override 
  14. // public void run() { 
  15. //      
  16. // threadLocal.set(j+"-----------"+Thread.currentThread().getId()+"-----"+threadLocal.hashCode()); 
  17. // Thread.currentThread().getId(); 
  18. // try { 
  19. // Thread.currentThread().sleep(1000); 
  20. // } catch (InterruptedException e) { 
  21. // // TODO Auto-generated catch block 
  22. // e.printStackTrace(); 
  23. // } 
  24. // System.out.println(threadLocal.get()+"----------"+Thread.currentThread().getId()+"------"+"-----"+threadLocal.hashCode()); 
  25. // } 
  26. // }).start(); 
  27. // }

ThreadLocal in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.