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

    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?

    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
    Cambridge, UK
    Rep Power


    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-16-2012, 11:00 PM
  2. Replies: 4
    Last Post: 04-03-2009, 06: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