Results 1 to 7 of 7
Thread: Can i mutate a lock object?
- 06-16-2011, 04:28 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Can i mutate a lock object?
Is there any problem with the following code? Someone told me the getter synchronization guard won't work but I don't know why.
Java Code:public class MyClass { private volatile JobStatus status = new JobStatus(); protected void setJoblStatus (...) { synchronized (status) { status.set(…); //mutating status } } public JobStatus getStatus() { synchronized (status){ return status; } } }
- 06-16-2011, 04:56 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Looks fine to me, I am far from an expert on concurrency, but client side locking should work assuming the status item doesn't use a private lock.
What was there reason for why the lock on getting the object wouldn't work? It may be acceptable to simply make the entire method synchronized(which synchronizes on "this")
You may want to make the instance variable final, you are never changing the actual object, just the object your are referencing, when possible, make variables final.Last edited by sunde887; 06-16-2011 at 05:03 AM.
- 06-16-2011, 07:19 AM #3Java Code:
public JobStatus getStatus() { synchronized (status){ return status; }
Get in the habit of using standard Java naming conventions!
- 06-16-2011, 07:21 AM #4
Correction to my correction: I was wrong... the synchronization in that getter does absolutely nothing except slow down your program by a negligible amount.
Get in the habit of using standard Java naming conventions!
- 06-16-2011, 07:32 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Without synchronization, isn't it possible to view the object in a stale state?
One thread begins changing the state of the object, then another thread gets the reference to the object, then the first thread finishes making a change.
I'm just asking for my own understanding. I'm understanding concurrency, but it's quite a bit more difficult than anything else I've seen in the language.Last edited by sunde887; 06-16-2011 at 07:35 AM.
- 06-16-2011, 07:47 AM #6
Yes, reading the fields of an object while holding the lock on that object ensures that the values are not stale... assuming the writes were also synchronized.
But in that getter, no fields of the object referenced by "status" are read or written in the synchronized block, so there's no point to the synchronization.
If you wanted to ensure that the reference being returned is fresh, you would synchronize on the object that contains the reference (i.e., "this"), not on the object that the reference refers to.Get in the habit of using standard Java naming conventions!
- 06-20-2011, 09:08 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Lock a folder
By nchouhan15@gmail.com in forum New To JavaReplies: 4Last Post: 03-13-2012, 09:33 AM -
lock on class
By rajinder5 in forum New To JavaReplies: 0Last Post: 10-13-2010, 11:08 AM -
lock on ".class" object
By rajinder5 in forum Threads and SynchronizationReplies: 0Last Post: 10-11-2010, 05:38 PM -
How to reserve/lock a record/object for update
By Steve11235 in forum Java TipReplies: 0Last Post: 01-12-2009, 04:46 PM -
object lock question
By simon in forum New To JavaReplies: 2Last Post: 08-01-2007, 05:36 PM
Bookmarks