You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Martin Gainty <mg...@hotmail.com> on 2007/02/09 20:55:35 UTC
volatile vs ThreadLocal
Good Afternoon All-
In a StrutsApplication is it best to use delcare variables with volatile OR
ThreadLocal to ensure variables are referenced in a ThreadSafe manner?
Thx,
Martin Gainty
______________________________________________
Disclaimer and confidentiality note
Everything in this e-mail and any attachments relates to the official
business of Sender. This transmission is of a confidential nature and Sender
does not endorse distribution to any party other than intended recipient.
Sender does not necessarily endorse content contained within this
transmission.
(mobile) 603-438-5053
_________________________________________________________________
Check out all that glitters with the MSN Entertainment Guide to the Academy
AwardsŽ http://movies.msn.com/movies/oscars2007/?icid=ncoscartagline2
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: volatile vs ThreadLocal
Posted by Martin Gainty <mg...@hotmail.com>.
I was not advocating volatile as a means of ensuring Thread safety
but more to the side effect of gaining performance
I am a strong personal advocate of synchronized but some of the leads dont
want to go that route
as it would be too intrusive to re-factor the existing base for the may
method calls
(In other words Im in partial agreement)
Thanks for responding,
Martin Gainty
______________________________________________
Disclaimer and confidentiality note
Everything in this e-mail and any attachments relates to the official
business of Sender. This transmission is of a confidential nature and Sender
does not endorse distribution to any party other than intended recipient.
Sender does not necessarily endorse content contained within this
transmission.
>
>Sorry Martin,
>but using volatile will not make your code any thread-safer.
>Volatile (if implemented at all by your VM) will ensure that all
>threads sees the "central" copy instead of their own copy. It won't
>prevent you from concurrent modification.
>
>Example:
>public MyThread{
> private boolean running;
> public void run(){
> while(running){
> ....
> }
> }
>
> public void stopThread(){
> running = false;
> }
>
> public static void test(){
> MyThread t = new MyThread();
> t.start();
> try{
> Thread.sleep(10000);
> }catch(InterruptedException e){}
> t.stopThread();
> }
>
>In this example the variable "running" should be declared volatile to
>ensure that the MyThread-thread will actually get the modified copy
>and stop (however using synchronized isRunning()/setRunning(boolean)
>methods is far better)
>
>For variables which are modified from many threads you can use
>synchronization or atomics (faster on sun).
>
>Btw, using ThreadLocals to avoid synchronization also seems weird :-)
>
>regards
>Leon
>
>
>On 2/10/07, Martin Gainty <mg...@hotmail.com> wrote:
>>after discussing a few scenarios I came upon the idea of
>>
>>volatile is to be used as a default declarator (if only for speed..)
>>ThreadLocal is to be used for complex DataObjects whose size may 'grow'
>>over
>>time
>>in this way ThreadLocal guarantees proper initialisation of the entire
>>object
>>Lighter (primarily primitive such as int,double) objects may live in
>>memory
>>as volatile
>>
>>Thanks Dave--
>>
>>Martin Gainty
>>
>>______________________________________________
>>Disclaimer and confidentiality note
>>Everything in this e-mail and any attachments relates to the official
>>business of Sender. This transmission is of a confidential nature and
>>Sender
>>does not endorse distribution to any party other than intended recipient.
>>Sender does not necessarily endorse content contained within this
>>transmission.
>>
>>
>>
>> >
>> >--- Martin Gainty <mg...@hotmail.com> wrote:
>> > > In a StrutsApplication is it best to use delcare
>> > > variables with volatile OR ThreadLocal to ensure
>> > > variables are referenced in a ThreadSafe manner?
>> >
>> >Which variables, and what types?! (Note mild alarm.)
>> >Are you attempting to share data, or ensure that it
>> >*isn't* shared? Where are you accessing the variable?
>> >
>> >d.
>> >
>> >
>> >
>> >
>> >____________________________________________________________________________________
>> >Looking for earth-friendly autos?
>> >Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
>> >http://autos.yahoo.com/green_center/
>> >
>> >---------------------------------------------------------------------
>> >To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >For additional commands, e-mail: user-help@struts.apache.org
>> >
>>
>>_________________________________________________________________
>>Valentine's Day -- Shop for gifts that spell L-O-V-E at MSN Shopping
>>http://shopping.msn.com/content/shp/?ctId=8323,ptnrid=37,ptnrdata=24095&tcode=wlmtagline
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
_________________________________________________________________
Turn searches into helpful donations. Make your search count.
http://click4thecause.live.com/search/charity/default.aspx?source=hmemtagline_donation&FORM=WLMTAG
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: volatile vs ThreadLocal
Posted by Leon Rosenberg <ro...@googlemail.com>.
Sorry Martin,
but using volatile will not make your code any thread-safer.
Volatile (if implemented at all by your VM) will ensure that all
threads sees the "central" copy instead of their own copy. It won't
prevent you from concurrent modification.
Example:
public MyThread{
private boolean running;
public void run(){
while(running){
....
}
}
public void stopThread(){
running = false;
}
public static void test(){
MyThread t = new MyThread();
t.start();
try{
Thread.sleep(10000);
}catch(InterruptedException e){}
t.stopThread();
}
In this example the variable "running" should be declared volatile to
ensure that the MyThread-thread will actually get the modified copy
and stop (however using synchronized isRunning()/setRunning(boolean)
methods is far better)
For variables which are modified from many threads you can use
synchronization or atomics (faster on sun).
Btw, using ThreadLocals to avoid synchronization also seems weird :-)
regards
Leon
On 2/10/07, Martin Gainty <mg...@hotmail.com> wrote:
> after discussing a few scenarios I came upon the idea of
>
> volatile is to be used as a default declarator (if only for speed..)
> ThreadLocal is to be used for complex DataObjects whose size may 'grow' over
> time
> in this way ThreadLocal guarantees proper initialisation of the entire
> object
> Lighter (primarily primitive such as int,double) objects may live in memory
> as volatile
>
> Thanks Dave--
>
> Martin Gainty
>
> ______________________________________________
> Disclaimer and confidentiality note
> Everything in this e-mail and any attachments relates to the official
> business of Sender. This transmission is of a confidential nature and Sender
> does not endorse distribution to any party other than intended recipient.
> Sender does not necessarily endorse content contained within this
> transmission.
>
>
>
> >
> >--- Martin Gainty <mg...@hotmail.com> wrote:
> > > In a StrutsApplication is it best to use delcare
> > > variables with volatile OR ThreadLocal to ensure
> > > variables are referenced in a ThreadSafe manner?
> >
> >Which variables, and what types?! (Note mild alarm.)
> >Are you attempting to share data, or ensure that it
> >*isn't* shared? Where are you accessing the variable?
> >
> >d.
> >
> >
> >
> >
> >____________________________________________________________________________________
> >Looking for earth-friendly autos?
> >Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
> >http://autos.yahoo.com/green_center/
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >For additional commands, e-mail: user-help@struts.apache.org
> >
>
> _________________________________________________________________
> Valentine's Day -- Shop for gifts that spell L-O-V-E at MSN Shopping
> http://shopping.msn.com/content/shp/?ctId=8323,ptnrid=37,ptnrdata=24095&tcode=wlmtagline
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: volatile vs ThreadLocal
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Martin,
Martin Gainty wrote:
> after discussing a few scenarios I came upon the idea of
> volatile is to be used as a default declarator (if only for speed..)
Volatile can do nothing but /decrease/ the speed of variable access.
Actually, most JVMs do nothing when you declare something as "volatile".
It's basically a deprecated concept in Java.
If the JVM did what it was /supposed/ to do, then every access of the
particular variable would have to go back to global memory and not the
thread's cache of data (with which you cannot interact). This can only
serve to slow things down.
> ThreadLocal is to be used for complex DataObjects whose size may
> 'grow' over time in this way ThreadLocal guarantees proper
> initialisation of the entire object Lighter (primarily primitive such
> as int,double) objects may live in memory as volatile
As far as I'm concerned, ThreadLocal variables should only be used when
your application is designed in such a way that you must "cheat" to pass
data from one method to another instead of actually passing them as
method arguments.
ThreadLocal has nothing to do with the way objects are stored in memory.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFzQ149CaO5/Lv0PARAv6QAJ0TJoJMDClf/2JeiFJJk6KbKDXDEQCgwmnn
lMDX6zJ7aSkJI8i7J3XL4+E=
=20GD
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: volatile vs ThreadLocal
Posted by Martin Gainty <mg...@hotmail.com>.
after discussing a few scenarios I came upon the idea of
volatile is to be used as a default declarator (if only for speed..)
ThreadLocal is to be used for complex DataObjects whose size may 'grow' over
time
in this way ThreadLocal guarantees proper initialisation of the entire
object
Lighter (primarily primitive such as int,double) objects may live in memory
as volatile
Thanks Dave--
Martin Gainty
______________________________________________
Disclaimer and confidentiality note
Everything in this e-mail and any attachments relates to the official
business of Sender. This transmission is of a confidential nature and Sender
does not endorse distribution to any party other than intended recipient.
Sender does not necessarily endorse content contained within this
transmission.
>
>--- Martin Gainty <mg...@hotmail.com> wrote:
> > In a StrutsApplication is it best to use delcare
> > variables with volatile OR ThreadLocal to ensure
> > variables are referenced in a ThreadSafe manner?
>
>Which variables, and what types?! (Note mild alarm.)
>Are you attempting to share data, or ensure that it
>*isn't* shared? Where are you accessing the variable?
>
>d.
>
>
>
>
>____________________________________________________________________________________
>Looking for earth-friendly autos?
>Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
>http://autos.yahoo.com/green_center/
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
_________________________________________________________________
Valentines Day -- Shop for gifts that spell L-O-V-E at MSN Shopping
http://shopping.msn.com/content/shp/?ctId=8323,ptnrid=37,ptnrdata=24095&tcode=wlmtagline
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: volatile vs ThreadLocal
Posted by Dave Newton <ne...@yahoo.com>.
--- Martin Gainty <mg...@hotmail.com> wrote:
> In a StrutsApplication is it best to use delcare
> variables with volatile OR ThreadLocal to ensure
> variables are referenced in a ThreadSafe manner?
Which variables, and what types?! (Note mild alarm.)
Are you attempting to share data, or ensure that it
*isn't* shared? Where are you accessing the variable?
d.
____________________________________________________________________________________
Looking for earth-friendly autos?
Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
http://autos.yahoo.com/green_center/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org