Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized ( object ) question

 
Brian Albin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Brian Albin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic