Results 1 to 2 of 2
Thread: [SOLVED] syncrhronized methods
- 06-14-2009, 02:42 PM #1
[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.
- 06-14-2009, 08:52 PM #2
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 controlDon't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-17-2012, 12:00 AM
- By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 07:36 AM
- By Java Tip in forum Java TipReplies: 0Last Post: 01-30-2008, 11:00 AM
- By Zensai in forum New To JavaReplies: 10Last Post: 12-03-2007, 06:31 AM
- By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 09:49 PM