Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By zserzs

Thread: synchronized(this){}

  1. #1
    zserzs's Avatar
    zserzs is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default synchronized(this){}

    All,

    I ran into the following question in a Java test:

    - Why the compiler is not removing this code?
    synchronized(this){}

    Does the question ask that it is not removed despite of the fact that we'd like to synchronize an empty statement?
    What is the correct answer?

    Thanks!

    Zs.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: synchronized(this){}

    The question is asking, why can't the compiler remove that, since it appears to do nothing? Presumably, the compiler can remove empty statements and code that won't be reached. Why can't the compiler remove that statement?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    zserzs's Avatar
    zserzs is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: synchronized(this){}

    Quote Originally Posted by KevinWorkman View Post
    The question is asking, why can't the compiler remove that, since it appears to do nothing? Presumably, the compiler can remove empty statements and code that won't be reached. Why can't the compiler remove that statement?
    Okay, makes perfect sense... But why?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: synchronized(this){}

    Quote Originally Posted by zserzs View Post
    Okay, makes perfect sense... But why?
    That's the assignment. Why do you think? What does the synchronized keyword do?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    zserzs's Avatar
    zserzs is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: synchronized(this){}

    Quote Originally Posted by KevinWorkman View Post
    That's the assignment. Why do you think? What does the synchronized keyword do?
    Okay, let's do it this way :)
    Here are my thoughts:
    the synchronized keyword makes it sure that there could be only one read/write operation at a time... so it might be the case that synchronized(this) synchronizes the current working object for I'm-not-sure-what-context and does it even if that block is empty...

    Is it super-stupid, or not that bad?

    Thanks,
    Zs.

  6. #6
    zserzs's Avatar
    zserzs is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: synchronized(this){}

    Kevin,
    I had lunch, I've been thinking on this...
    Here are my thoughts:

    Given a point in time where we are at the line synchronized(this){}. Now, all previous locks has to be released, so this line basically does nothing, but separates code before and after it. I've never seen this in practice, though.... Am I closer?

    Zs.
    Whired likes this.

  7. #7
    Whired is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Re: synchronized(this){}

    Quote Originally Posted by zserzs View Post
    all previous locks has to be released
    Seems like a good answer to me? A moniterenter would mean waiting until the lock object is unmonitored. I can't think of why someone would need to, but removing this would seem to change how the program works.

Similar Threads

  1. Problems with synchronized
    By BigHotCuppa in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-20-2010, 08:11 PM
  2. Replies: 1
    Last Post: 09-08-2010, 04:08 PM
  3. Synchronized(this)?
    By kiza in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-07-2009, 01:20 PM
  4. synchronized
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-28-2007, 10:33 AM
  5. doubt about synchronized
    By simon in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 03:49 AM

Posting Permissions

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