Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Re: volatile vs ThreadLocal

Leon Rosenberg

2007-02-09

Replies:

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 <mgainty@(protected):
> 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 <mgainty@(protected):
> > > 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@(protected)
> >For additional commands, e-mail: user-help@(protected)
> >
>
> _________________________________________________________________
> 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@(protected)
> For additional commands, e-mail: user-help@(protected)
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.