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