I beleive I'm still failing to get the problem across. I'm not saying there is little concurrency. I'm saying that the most optimal and correct
I have found is to use the synchronized method approach and avoid any handoff. What I'm asking is whether there is a better. It's not a newbie question.
I know how synchronized works and if you look at the code I've posted I am not relying on the wait order at all. It's not getting lucky. Its the more concurrent code (which uses an atomic and concurrent hashmap) which is getting lucky 99.99% of the time. Using that code the flow performs much faster under heavy concurrent load but tests are showing there is this tiny window where I can be left with an item in the queue when the processing comes to an end.
I did think about time stamping but that creates a problem later on when you try to serialize beceause of the gaps in timestamps. If the sequencer recieves a message with a timestamp T, and then recieves a messge with timestamp T+10, it doesn't know if this message has overtaken another message with timestamp T+5. I beleive the atomic long is doing a very good job of assigning sequencial numbers (with no gaps) and the concurrency is great so that part of the design looks good for now.
The are no network paths in the flow I'm trying to optimize. The problem is all in a single process.