This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
in this code ThreadA contains the main thread and ThreadB has a thread for calculation.
Now when b.start() is called ,the ThreadB enters into synchronised block and has a lock of the object b; then how can main thread enters into synchronised(b) and wait method is called?
as every object has only one lock and in this case that is aquired by ThreadB so how can main thread can call wait method (as thread must aquire lock on object to call wait method)
Now when b.start() is called ,the ThreadB enters into synchronised block and has a lock of the object b; then how can main thread enters into synchronised(b) and wait method is called?
This is not entirely correct. The main thread merely starts the thread B with the the call to the start() method. It does not call the run() method -- thread B does that.
In starting a thread, it will schedule the thread to run, which may or may not run immediately. In fact, in most cases, the answer is that it will not be running. For you, as a developer, you should design your code to make no assumptions. You should design your code to work regardless if thread B or the main thread runs first.