How do you pause a thread until a condition becomes true without busy waiting? For example, suppose I'm checking to see whether
playerOne.isWalking()
Is going to be true and I want to run some code when it does become true. Again suppose playerOne is an instance of the class Player which then has the function isWalking(). I do not have access to the what makes isWalking() become true. So everytime I want to check to see if isWalking() is true, I have to call the method from an instance of player explicitly.
I've tried using Wait/Notify in synchronized blocks but that wouldn't ever work because I would have to manually notify the Thread to wake up when isWalking() becomes true and I don't know when it will become true.
I've tried using the observer pattern to make Player a subclass of observable so that I can call the update method when isWalking() becomes true but again I don't know when that will happen.
I've even tried using Propertys but that wouldn't work either.
Is this even possible to check without busy-waiting? Constantly polling and calling that function to see if it is true, then if it is true, execute the code.
This was a bad solution I made involving busy-waiting:
Busy-Waiting solution