Basic synchronization questions
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   miki_Vis
Posted On:   Wednesday, January 25, 2012 04:15 AM

Hi, I have some basic questions concerning synchronization: 1. Do I have to worry about multiple threads accessing the same object WITHOUT modifying it? (For example, multiple threads using a Map's get(...) method) 2. Let's say I have a synchronized method that returns an object (Let's say a simple getter). Now let's say that two threads access the same object, after they call this method. The fact that the method is synchronized is useless since both threads use the same object AFTER the getter is called. So, in order to avoid this, should I clone the object in the synchronized object, and only then return it (e.g. via a copy constructor), or am I missing something here? 3. Last   More>>

Hi,

I have some basic questions concerning synchronization:




1. Do I have to worry about multiple threads accessing the same object WITHOUT modifying it? (For example, multiple threads using a Map's get(...) method)



2. Let's say I have a synchronized method that returns an object (Let's say a simple getter). Now let's say that two threads access the same object, after they call this method. The fact that the method is synchronized is useless since both threads use the same object AFTER the getter is called. So, in order to avoid this, should I clone the object in the synchronized object, and only then return it (e.g. via a copy constructor), or am I missing something here?



3. Last but not least: Does a synchronized segment synchronizes the pointer or the object it refers to? if it refers to the object, then what's the meaning of the following code:

			
public void setEmployee(Employee emp){
synchronized(m_emp){
m_emp = emp;
}
}


After all, we only change here the object that the pointer m_emp refers to! So if we synchronize the object, we really do nothing here.. Am I missing something?


Thanks!    <<Less

Re: Basic synchronization questions

Posted By:   Anonymous  
Posted On:   Tuesday, February 7, 2012 05:14 AM

1.For readning a shared resource we dont need Synch

2.You may have to use Synch block to lock the returned object not the getter API

3.it locks the object only not the pointer
About | Sitemap | Contact