Results 1 to 2 of 2
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] syncrhronized methods

    "If count is an instance of SynchronizedCounter, then making these methods synchronized has two effects:

    First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

    Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

    Note that constructors cannot be synchronized — using the synchronized keyword with a constructor is a syntax error. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed." (1).

    1. What exactly is a "happens before" relationship?
    2. Is it necessary to use a condition.signal() or condition.signalAll() the other methods to wake up awaiting threads, or are other synchronized methods waiting upon the synchronized method queued automatically? Is this question at all relevant to synchronized methods?

    NOTES:
    1. Synchronized Methods (The Java™ Tutorials > Essential Classes > Concurrency)
    2. Core Java Volume I Pg. 747 (761) ISBN: 978-0-13-235476-9.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    if X and Y are in a happens-before relationship then "X happens before Y"

    the "synchronized" keyword automatically deals with wait/notify cycles on the method or code block so you don't have to worry there, unless you need more complex or fine-grained control
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  3. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 11:00 AM
  4. methods
    By Zensai in forum New To Java
    Replies: 10
    Last Post: 12-03-2007, 06:31 AM
  5. Methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:49 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
  •