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.
I have two classes running on different threads and both use the same resource.
"isUpdateNeeded" is the var that I want to update (the lock). The method "setUpdateVar()" change the value of this variable to true and notify the thread "UpdateVar" to use this variable. The issue occurs when notifyAll() is called:
I assume isUpdateNeeded is a Boolean (or a boolean that autoboxes to a Boolean). If this is the case, then changing the value from true to false (or vice-versa) actually changes the Object referred to by isUpdateNeeded. IE When true, is is Boolean.TRUE and when false it is Boolean.FALSE, two separate Objects, which means you can't synchronize/notify between them.
To simplify things you should have a separate lock that can't change:
Joined: Apr 06, 2009
Thanks a lot. It works great with your solution!
Thanks for the explanation