In that I have 3 thread which should be run sequentially.
Well you're not running sequentially - you're running the three concurrently, which is the nature of what happens when you start three separate threads.
Those threads aren't synchronizing against each other at all. It doesn't really sound like you should have three threads at all, given your requirements.
Additionally:
- You don't have any
synchronized
blocks; bothwait()
andnotify()
can only be called by a thread which owns the monitor on which they're called - Using
wait()
andnotify()
on instances ofThread
is strongly discouraged, as the internals ofThread
use it themselves. I suggest you create a separate object just to synchronize/wait/notify on. - Your exception handling completely ignores the exception, which has been hiding the first problem from you. Never ignore the exception - and try to avoid catching bare
Exception
at all, in general.