Dear Batra,
Welcome to Javaranch.
You are synchronizing on instance of thread. If we check the
JoinMethodDocumentation, you will see that Thread instance
gets notified when run() method completes. Check the documentation of the join() method.So basically, you execute one explicit notify call
(the one we see in the code), and another implicit notifyAll when the thread is done. We shouldn't synchronize on the Thread object.
Thread toString() methods returns names of all threads in the ThreadGroup, not only name of current Thread. You could even remove your explicit notify
and the behavior wouldn't change due to the implicit notifyAll at the end. Check the modified code below.
HTH,
Ben