Question, more than one thread of a process program could read sequentially from one static java.util.arrayqueue? If three threads of a process program are running and three data object arrive into the arrayqueue, each thread will process one data object or just one thread will process all three data object
Question, more than one thread of a process program could read sequentially from one
Share
You’re describing the producer-consumer pattern.
A few points
If you spawn three threads and they each loop, reading off the same queue then they will all try to consume the items on the queue. There’s no guarantees on how many items each thread will consume. This depends entirely on the scheduler of the threads in the JVM (or at the OS level). Generally speaking though if a large number of similar items are added to the queue each thread will consume roughly the same amount of items.
ArrayQueue is not a good choice for this use-case as it is not threadsafe. A better choice would be ConcurrentLinkedQueue or ConcurrentBlockingQueue
To answer your question directly, yes you can have multiple threads reading from the same queue, but please consider the above.