Results 1 to 2 of 2

Thread: Object's lock

  1. #1
    Join Date
    Aug 2013
    Rep Power

    Default Object's lock

    From the Head First Java book:

    The locks are not per method, they are per object If an object has two synchronized methods, it does not simply mean that you can't have two threads entering the same method. It means you can't have two threads entering any of the synchronized methods
    Unable to understand the part in bold.

    Phew! Looks like the first statement in bold does not go with the second statement. The more I read the part in bold, the more I get confused. An example will help.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Object's lock

    I agree it is not that well written. And I do not agree with it entirely. But essentially, is saying that you can have both synchronized and unsynchronized methods in a class. Two or more threads may enter the unsynchronized methods whenever they please. However, the synchronized methods use locks to ensure that only one thread may enter the method. That thread then "owns" the lock. No two threads may enter a synchronized method or block at the same time.

    I would also say that no two threads may exist in the same method at the same time unless one of the threads has released the lock. It is possible for one thread to enter a wait state inside a synchronized method and allow another thread to enter. But both threads may not be active in a synchronized method or block at the same time.

    Caveat: I am not an expert on threads so I would encourage others to enter the discussion.

    Last edited by jim829; 08-25-2013 at 05:21 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Can i mutate a lock object?
    By yipking in forum Threads and Synchronization
    Replies: 6
    Last Post: 06-20-2011, 08:08 PM
  2. lock on ".class" object
    By rajinder5 in forum Threads and Synchronization
    Replies: 0
    Last Post: 10-11-2010, 04:38 PM
  3. 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
  4. lock a file
    By flaca in forum New To Java
    Replies: 3
    Last Post: 08-19-2008, 03:00 PM
  5. object lock question
    By simon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:36 PM

Posting Permissions

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