This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Using ThreadLocal for setting data in a WebApplication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using ThreadLocal for setting data in a WebApplication" Watch "Using ThreadLocal for setting data in a WebApplication" New topic
Author

Using ThreadLocal for setting data in a WebApplication

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Hello ,

I had purposely posted this question in Servlets ,as i want to map it to the Web Environment.

What advantage i can get setting the data in a ThreadLocal . AS per docs this says that the data can be extracted by only a particular Thread . Please tell me how this is possible ??

and how this is different from setting it in a session/request.


Thanks in advance.


If you want something you never had do something which you had never done
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Using ThreadLocal, that means data is local to thread, you can have thread-safety.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thanks for the response .

Can you please provide me an example where this would be suitable (Setting the data for a Thread )

Thanks in advance .
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

I see no advantage to using thread-local storage. What problem are you trying to solve that requires this?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
The advantage would be
data can be more secured know.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

RaviNada Kiran wrote:The advantage would be
data can be more secured know.

I have no idea what you mean by this.
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Container allocates each request in a web application with a new thread, creating one or selecting one available from thread pool.

So it doesn't makes any sense to store the data into ThreadLocal wrt web application.

Advantages, I dont feel wrt web app.




RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thanks a lot for your nice explanation.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using ThreadLocal for setting data in a WebApplication
 
Similar Threads
get applicationContext at Business layer
Advantage of Threadlocal over session object
Using ThreadLocal
Some ThreadLocal issues
Can I get the datasource without referencing the actionServlet???