aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread local Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread local" Watch "Thread local" New topic
Author

Thread local

liliane fahmy
Greenhorn

Joined: Aug 24, 2012
Posts: 22
Is the threadLocal is the same concept of synchronization ?
Please I need reply
Thanks and best regards
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

No, they are not the same. You use synchronization to protect code blocks, and often to safely share the same data across multiple threads. You use ThreadLocal to have a single reference to access different data for each thread.


Steve
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Thread local is just like a scope, it means the scope is limited to thread, i.e. every thread will have its own data.
Synchronization in other hand is used for data which is shared and not its local.
srikanth kasa
Greenhorn

Joined: Aug 28, 2012
Posts: 2
Thread Synchronization:
When a Thread is already acting on one object,preventing any other thread from acting on the same object is called "Thread Synchronization" or "Thread Safe". The Object on which the threads are synchronized is called "Synchronized Object".Thread Synchronization is recommended when multiple threads are used on the same object(in multiThreading).

Thread Locale:
Thread locale is a scope of access like,global Scope,local scope to a specific Thread,which is accessing an object.

Example:
I can point out one use case where I used thread local. Consider you have a Servlet which calls some business methods. You have a requirement to generate a unique transaction id for each and every request this servlet process and you need to pass this transaction id to the business methods, for logging purpose. One solution would be passing this transaction id as a parameter to all the business methods. But this is not a good solution as the code is redundant and unnecessary.

To solve that, you can use Thread Local. You can generate a transaction id (either in servlet or better in a filter) and set it in the Thread Local. After this, what ever the business method, that this servlet calls, can access the transaction id from the thread local.

This servlet might be servicing more that one request at a time. Since each request is processed in separate thread, the transaction id will be unique to each thread (local) and will be accessible from all over the thread’s execution (global).
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

On Related note ThreadLocal is not as simple as it look like, In correct usage of ThreadLocal can create memory leak in Java. Especially in J2EE environment where your code runs on a managed environment and application server's thread are used to serve request, which lives longer than http request.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

Javin Paul wrote:On Related note ThreadLocal is not as simple as it look like, In correct usage of ThreadLocal can create memory leak in Java. Especially in J2EE environment where your code runs on a managed environment and application server's thread are used to serve request, which lives longer than http request.


This is not completely correct -- as the Thread instance does use a WeakReference to the ThreadLocal intance. This means that if the ThreadLocal instance becomes unreachable because the http request is out-of-scope, the memory will be eventually released. Having said that, there does seem to be some delay in doing so -- it does look like it takes time for it to happen (and I didn't look into why ... but my speculation is the weak reference only clears the key, and it will take another access (by a different threadlocal obviously) to clear the values).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread local