Reputation: 908
I have data structure (i.e. queue, stack or list). There is infinite loop (in thread 1) that inserts objects into that data structure, and there is another infinite loop (in thread 2) that takes objects from that data structure and does some processing on it. I don't want the processing loop to block the insert loop.
How can I do this correctly in Java, to share a single data structure between two threads with synchronization and concurrent read/write from the two different threads? Is there any data structure in java concurrent API (java 7) that I can use? is there any recommendation to solve this issue?
Upvotes: 0
Views: 116
Reputation: 8246
What you are looking for is a non-blocking data structure, say ConcurrentLinkedQueue. It will accept entries to the queue and requests to it without blocking either, making it very simple to use without implementing anything fancy.
Further Reading (Into the theory behind non-blocking)
Upvotes: 5
Reputation: 66
Java's ConcurrentLinkedQueue seems like your best bet for a FIFO ordered queue.
Upvotes: 3