[concurrency-interest] RE: Synchronization of data read by multiple threads

Ryan LeCompte Ryan.LeCompte at pangonetworks.com
Sat Oct 29 09:55:47 EDT 2005


Hello all,

 

As a follow-on to my original question
 I have another scenario where I’m not entirely sure if synchronization/volatile is needed. Let’s say we have the following:

 

Class Test implements Runnable {

   Private boolean instantiated = false;

    

   Public Test() {

      Instantiated = true;

   }

 

   private boolean isInstantiated() {

       Return instantiated;

    }

 

     Public void run() {

         If (isInstantiated()) {

             // do something

         }

     }

}

 

And then somewhere in the main thread we do the following:

 

Test t = new Test();

Thread th = new Thread(t);

t.start();

 

Is it possible that the new thread that is running, which it invokes isInstantiated() will see the value “false” instead of “true” ? Does it have to be declared volatile? Do all variables of the class have to be declared “volatile” in order for the newly instantiated thread to see their most recently written values? Note that the only time the class is used by different threads would be in the main thread when it is instantiated (new Test()), and then after that the only thread ever reading that particular value would be the newly instantiated Thread.

 

Ryan

 


--------------------------------------------------------------------------------

From: Ryan LeCompte [mailto:ryan.lecompte at pangonetworks.com] 
Sent: Tuesday, October 25, 2005 9:17 AM
To: 'concurrency-interest at altair.cs.oswego.edu'
Subject: Synchronization of data read by multiple threads

 

Hello all,

 

I know that one has two options for “protecting” data that can be potentially read/written by multiple threads. In order for the main thread to always see the most recently written value, one must declare the variable as “volatile” or synchronize all access to it, such as:

 

synchronized void stop() {

   stopped = true;

}

 

synchronized boolean isStopped() {

   return stopped;

}

 

However, if the “synchronized” approach is taken, does it have to be at such a granular level? Or can it suffice that whenever the variable “stopped” is used, that it’s at least protected by SOME lock? For example, if “stopped” is only directly referenced in three methods that perform various operations, can all three methods be declared as “synchronized” and the above two methods (stop() / isStopped()) simply removed? Or do we always need to have “synchronized accessors” for the variable in question? Also, what happens if there are three methods that use the “stopped” variable, but they are using different locks? For example, let’s say method1 uses “stopped” in a synchronized block on LOCK1, and method2 uses “stopped” in a synchronized block on LOCK2, and method3 uses “stopped” in a synchronized block on LOCK3. Will we still have the same effect as simply declaring the variable as “volatile” here?

 

Thanks,

Ryan

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20051029/4e82b288/attachment.htm


More information about the Concurrency-interest mailing list