It's not a secret anymore!*
The moose likes Threads and Synchronization and the fly likes Issue with wait/notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Issue with wait/notify" Watch "Issue with wait/notify" New topic
Author

Issue with wait/notify

Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

Hi,
I'm trying to get some code working which should do something like loop through a list of tradeIds and add a value for each trade if it doesn't have one, do nothing if it has been processed, and wait if it is being currently processed then wake up and continue.

My code looks like:

Trade class


the thread:



and the main method class


the problem I'm having is that once a thread goes to sleep it never wakes up, I'm calling notifyAll after a trade is processed but nothing happens. This is probably an embarrasing schoolboy error but I'm stumped!

Thanks
Willy
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18486
    
  40

will myers wrote:
the problem I'm having is that once a thread goes to sleep it never wakes up, I'm calling notifyAll after a trade is processed but nothing happens. This is probably an embarrasing schoolboy error but I'm stumped!


You are using the "this" object for notification, which is the runnable instance for the thread. If you look at your init code, you will see that the three thread have different runnable instances. IOWs, the three threads are using different synchronization and notification objects.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

Thanks Henry, I knew it was something dumb!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with wait/notify
 
Similar Threads
CoOrd Class
ConcurrentHapMap size not as expected when put done using Multiple Threads
pls suggest me a good style of programming
cannot understand the exception in my code,please help
finding closest point