I have a simple blocking queue containing Integers. In a multi thread environment I am taking and adding elements to the the back of the queue.
BlockingQueue<Integer> test = new LinkedBlockingQueue<Integer>();
The goal is to have adding in order.. and taking be in the same order as well. On the 5th line of my program output, 3 is somehow at the front of the queue before 2 is, despite it appearing that 2 was added first. All of these were added in a single threaded environment, so I know the code to be added is executed in order
Add: 1
Add: 2
Add: 3
Take: 1
Take: 3
Add: 4
Take: 2
Take: 4
Is this unexpected behavior? Right now I am only tested in Single Threaded environments so I would expect the de-queing order to stay consistend with the order that was added.
Is there another thread safe datastructure that I should be using instead?
Thanks for the help in advance