This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi all, Say, I have N threads competing access and modify an object. All I have to do is qualify all the methods of that object with 'Synchronized' keyword. This would automatically force 'synchronous' access to that object. None of the accessing threads need to wait/notify. Am I correct? Thanks in advance. Bala.
If all of the methods of an object are synchronized, and all of the data members of that class are private, and the class doesn't make any assumptions about the order in which its methods should be called, then yes, you can be reasonably confident that the object can be used in a multithreaded environment safely (of course, you can get deadlocks -- but you won't get corrupted data.) The various threads can act as if no other threads exist, and don't need to use wait() or notify() for anything.
All I have to do is qualify all the methods of that object with 'Synchronized' keyword. This would automatically force 'synchronous' access to that object.
read retraction below! Not quite. Using the 'synchronized' keyword on a method only controls multiple threads' access to that method. It does not prevent other threads from accessing other methods in the same object, even if those other methods are synchronized. You have to use synchronized blocks within methods that block on 'this' to insure only one thread is altering an object's state at a particular time. read retraction below! [ February 20, 2004: Message edited by: Joe Ess ]
Using the 'synchronized' keyword on a method only controls multiple threads' access to that method. I don't believe this is correct. No two synchronized nonstatic methods may execute simultaneously if invoked from the same object. Similarly, no two synchronized static methods may execute simultaneously, if they're from the same class. Confusingly though, two synchronized methods from the same class/instance may exectue simultaneously if one is static and the other is not. That's because
is equivalent to
is equivalent to
Static and nonstatic methods sync on two different things, so interactions between them can be complex. Typically it's best to avoid this possibility entirely - if you have synchronized nonstatic methods in a class, don't allow any synchronized static. Or vice versa.
Originally posted by Jim Yingst: I don't believe this is correct. No two synchronized nonstatic methods may execute simultaneously if invoked from the same object.
You are correct. I went back to the code I thought was exhibiting such behavior and it turns out that multiple threads are able to invoke synchronized methods simultaneously because invoking wait() releases the lock on the object. A simplified example would be a Queue class where get() and put() are synchronized and get() blocks with wait() if there are no objects in the queue. I had gotten to thinking this proved my previous point, but as I said, when the consumer thread blocks on wait() he releases the synchronized lock, allowing the producer to enter the put() method.