The javadoc for wait()
states
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object
So when you do
b.wait(10);
the current thread releases the synchronized
it has on b
and your other thread can therefore acquire it in the run()
method coming from
b.start();
The total
starts increasing. When 10ms is up, the main thread reacquires the lock on b
(assuming the run()
completes) and prints out the total. Note that your total
will most likely overflow.