File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized ( object ) question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized ( object ) question" Watch "synchronized ( object ) question" New topic
Author

synchronized ( object ) question

Brian Albin
Greenhorn

Joined: Mar 02, 2004
Posts: 24
I have a decent understanding of using synchronized methods and the wait/notify protocol.
My problem is using the synchronized ( object ) approach. See the code framework below for the example problem I am using to learn this topic. The code is lengthy so hopefully the framework explains my problem. It seemed logical to me to sync on the cubbyhole object to ensure that teller's put things there and accounts get things from there in a coordinated fashion. My code compiles, but complains when run that the current thread is not the owner. If anyone, understands synchronized ( object ) I would appreciate your help.
Thanks, Brian

[ March 23, 2004: Message edited by: Brian Albin ]
[ March 23, 2004: Message edited by: Brian Albin ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

My guess is that you're calling bare, unqualified wait() and notify() when you meant to call aCubbyholeObject.wait() and aCubbyholeObject.notify(). You have to signal through the object you're synchronized on, whether it's the implicit "this" of a synchronized method, or an object you're explicitly synchronizing on.
A synchronized method
synchronized void foo() ...
is essentially identical to


[Jess in Action][AskingGoodQuestions]
Brian Albin
Greenhorn

Joined: Mar 02, 2004
Posts: 24
Ernest,
Below is the entire code. The program outputs that the Teller has "put" the first transaction, but the Account thread justs wait and never sees the flag condition satisfied to "get" the transaction.
Edit To Original Post
---
I found the problem and corrected the code below. It now works fine. Ernest, thanks for your reply. You were correct in your guess about a bare wait( ) and bare notifyAll( ).
Brian

[ March 23, 2004: Message edited by: Brian Albin ]
[ March 23, 2004: Message edited by: Brian Albin ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized ( object ) question