I have a program which expects commands from 2 sources.
The other is a socket interface and the other is an event listener.
After setting up the listeners, the program waits for commands by calling:
wait();
The commands, when they arrive are added to (appended to the end of) a Vector using a synchronized method, which after adding the command calls
notify();
When that happens, the execution continues from the wait()-call and handles the contents of the command Vector, starting from the first element. To test the functionality, I added a Thread.sleep(5000) in the handling method so the handling would take at least 5 seconds.
Then I generated commands from both sources.
It turns out that the commands are not handled in the order which they arrived! The ones that arrive as events end up being handled first, even if a command from socket arrived first.
What could cause this? Is my design broken?
To me it sounds as you want to use a BlockingQueue. Let the sources add commands to it, and let the handler execute them in order.