Threadlocal v/s Synchronization

royjayd
royjayd used Ask the Experts™
on
hi guys

I was reading through Threadlocal v/s Synchronization and comtemplating which one to use.
Is threadlocal an alternative to synchronization  when it comes to scalability in high performance systems?
thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Java Developer
Top Expert 2010
Commented:
Awarded 2011
Awarded 2011
Commented:

Author

Commented:
can you guys post your knowledge in simple terms rather than pointing me to a link?

thanks.
Mick BarryJava Developer
Top Expert 2010

Commented:
I wouldn't think about it as an alternative for synchronisation. Instead think of it as another tool for dealing with threading issues. Understanding ThreadLocal and synchronisation will help you to determine which is the most appropriate in different scenarios.
For example if you have a huge objects that many threads are accessing then ThreadLocal may not be ideal as you are creating a new instance for each thread.
ThreadLocal is also not going to help you when your threads need to share data, thats more a job for synchonisation.
Mick BarryJava Developer
Top Expert 2010

Commented:
In general use ThreadLocal when you have multiple threads that each need their own copy of local data.
And synchronisation when multiple threads need to share some piece of data.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial