Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Some questions about ThreadLocal

Reply
Thread Tools

Some questions about ThreadLocal

 
 
lightning
Guest
Posts: n/a
 
      08-01-2008
In javadoc:
Each thread holds an implicit reference to its copy of a thread-local
variable as long as the thread is alive and the ThreadLocal instance
is accessible; after a thread goes away, all of its copies of thread-
local instances are subject to garbage collection (unless other
references to these copies exist).


It seems only when thread "goes away",the threadlocals of that thread
will be cleaned.
But what about when using threadpools?
For example:
Executors.newFixedThreadPool()


In TOMCAT,the threadlocals works fine because framework such dwr ,
struts2 or spring transactionmanager using threadlocals to store many
things.How did it work?
Will it work fine no matter no matter Tomcat is using threadpool or
using singlethread mode?
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      08-01-2008
lightning wrote:

> In TOMCAT,the threadlocals works fine because framework such dwr ,
> struts2 or spring transactionmanager using threadlocals to store many
> things.How did it work?


I'm not sure what you are trying to say. Can you give some code as an
example?

Regardless, I'm pretty sure that if a thread completes, it cannot be
used again. Therefore I assume that Executors such as FixedThreadPool
have some way of keeping a thread "open" and alive, and not allowing it
to terminate. So ThreadLocal objects should persist.


> Will it work fine no matter no matter Tomcat is using threadpool or
> using singlethread mode?


Single threaded mode is best avoided under all circumstances. Just
don't use it. I have no idea what it might do to thread local objects
under the various Java EE containers.
 
Reply With Quote
 
 
 
 
Kevin McMurtrie
Guest
Posts: n/a
 
      08-01-2008
In article <Ecykk.15965$(E-Mail Removed)>,
Mark Space <(E-Mail Removed)> wrote:

> lightning wrote:
>
> > In TOMCAT,the threadlocals works fine because framework such dwr ,
> > struts2 or spring transactionmanager using threadlocals to store many
> > things.How did it work?

>
> I'm not sure what you are trying to say. Can you give some code as an
> example?
>
> Regardless, I'm pretty sure that if a thread completes, it cannot be
> used again. Therefore I assume that Executors such as FixedThreadPool
> have some way of keeping a thread "open" and alive, and not allowing it
> to terminate. So ThreadLocal objects should persist.


ThreadLocal's storage has a WeakReference to the Thread as a key. That
means that the key lasts exactly as long as the Thread object, never
longer or shorter. The thread pointer could be re-used at the OS-level
but that's OK because it's guaranteed that no old Java references exist.

ThreadLocal should only be used as a last resort. It's prone to causing
mysterious side-effects and memory bloating. If you must use one, put
just one at a very high level and use it to store a session manager.

>
> > Will it work fine no matter no matter Tomcat is using threadpool or
> > using singlethread mode?

>
> Single threaded mode is best avoided under all circumstances. Just
> don't use it. I have no idea what it might do to thread local objects
> under the various Java EE containers.


--
Goolge is a pro-spamming service. I will not see your reply if you use Google.
 
Reply With Quote
 
shortcutter@googlemail.com
Guest
Posts: n/a
 
      08-01-2008
On 1 Aug., 10:02, Kevin McMurtrie <(E-Mail Removed)> wrote:
> In article <Ecykk.15965$(E-Mail Removed)>,
> *Mark Space <(E-Mail Removed)> wrote:
>
> > lightning wrote:

>
> > > In TOMCAT,the threadlocals works fine because framework such *dwr ,
> > > struts2 *or spring transactionmanager using threadlocals to store many
> > > things.How did it work?

>
> > I'm not sure what you are trying to say. *Can you give some code as an
> > example?


Same here, the question - if any - is not clear to me. ThreadLocals
can of course be used in the presence of thread pools. You just have
to be aware of the fact that their life time may exceed that of the
current "request" (whatever that is in a particular application).

> > Regardless, I'm pretty sure that if a thread completes, it cannot be
> > used again. *Therefore I assume that Executors such as FixedThreadPool
> > have some way of keeping a thread "open" and alive, and not allowing it
> > to terminate. *So ThreadLocal objects should persist.

>
> ThreadLocal's storage has a WeakReference to the Thread as a key.


This is not true. The weak reference points to the ThreadLocal
instance in order to make an entry garbage collectible when the
ThreadLocal instance can be collectible. The map itself is a member
of Thread so this is collected if the Thread is collected.

> *That
> means that the key lasts exactly as long as the Thread object, never
> longer or shorter.


The key can (and most likely will in the case of a static ThreadLocal)
last longer than a thread.

> ThreadLocal should only be used as a last resort. *It's prone to causing
> mysterious side-effects and memory bloating.


I would agree saying that you should be knowing what you are doing.
But I would not necessarily say this is a "last resort" only. It all
depends. It's a tool like many others with advantages and
disadvantages.

> *If you must use one, put
> just one at a very high level and use it to store a session manager.


The benefit of this session manager being?

Cheers

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem: what do you think that cache some object in ThreadLocal under j2ee.servlet.struct sods Java 0 05-29-2006 09:15 AM
Double-Check idom, ThreadLocal and volatile Timo Nentwig Java 1 05-06-2006 04:39 PM
J2EE (websphere) + Hibernate + ThreadLocal no Java 3 03-06-2006 07:50 PM
Object creation vs. ThreadLocal retrieval Ian Pilcher Java 3 10-29-2005 12:59 PM
Can you implement a Singleton within J2ME Apps, without using ThreadLocal? Steve Jasper Java 5 11-20-2003 06:31 PM



Advertisments