I think you have some serious holes in your understanding. You've declared a Thread
Thread b = new Thread();
and started it in the constructor
b.start();
That Thread
will start and die right away since it has no Runnable
attached to it.
It just so happens that when a Thread
dies, it calls notify()
on itself and since you are synchronized
on that same Thread
object, your wait()
ing thread will be awoken. You also have a race here. If the Thread
ends before the main thread reaches the wait()
, you will be deadlocked.
Also, there is no reason for run()
to be called, which is why total
remains 0.
Any object can be synchronized
on, it doesn't have to be a Thread
. And since Thread
has that weird behavior of notify()
ing itself, you probably shouldn't use it.
You should go through both the Thread
tutorials and the synchronization tutorials.