Results 1 to 7 of 7
  1. #1
    yipking is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default 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;
     }
     }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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 04:03 AM.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Java Code:
    public JobStatus getStatus()  {
     synchronized (status){
     return status;
     }
    This doesn't really do anything. What are you trying to accomplish?
    Get in the habit of using standard Java naming conventions!

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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!

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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 06:35 AM.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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!

  7. #7
    yipking is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Lock a folder
    By nchouhan15@gmail.com in forum New To Java
    Replies: 4
    Last Post: 03-13-2012, 08:33 AM
  2. lock on class
    By rajinder5 in forum New To Java
    Replies: 0
    Last Post: 10-13-2010, 10:08 AM
  3. lock on ".class" object
    By rajinder5 in forum Threads and Synchronization
    Replies: 0
    Last Post: 10-11-2010, 04:38 PM
  4. How to reserve/lock a record/object for update
    By Steve11235 in forum Java Tip
    Replies: 0
    Last Post: 01-12-2009, 03:46 PM
  5. object lock question
    By simon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •