hi folks, could you please help me with interpreting the followin codes as to what's going on there?
i have hard time understanding wait() and notify() here. As shown in the codes, i think when start() is invoked b starts invoking run()and the main thread go ahead runing codes right after b.start(). but we want b to sum 1 - 10 first and have main thread print that sum so we have to have main thread wait. instead, b is waiting here. i don't really understand this. i thought b is spawned and has gone to invoke run() not wait(). can you please explain me this process? thanks.
The key is that both are synchronizing on the same object, so when you call b.wait() in the main thread it goes into the b's wait pool until notify or notifyall() is called on it which is exactly what happens in the run method when the loop finishes.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
The overall mission is to change the world. When you've done that, then you can read this tiny ad: