简体   繁体   中英

Two queues and different threads

I have 2 queues A and B. Main thread is responsible for filling the queues. There is a threadpool of 3 threads responsible for reading from queues. Both Queues are ArrayBlockingQueue. Lets say main thread is filling the queues in this way (A1,B1),(A2,B2),(A3,B3),(A4,B4) and so on. A1 and B1 collectively makes a whole data. Is it possible for a any thread to read data like (A3,B4) at any point. If yes then how can i avoid it. I want every thread to read data the altogether. eg thread1 should read both (A1,B1) and thread2 (A2,B2) and so on.

As so often, the answer is: depends .

Of course you can implement something that would follow the outlined approach and that in the end is "correct". But: getting there will be hard .

A much more straight forward way: fix your broken design. If the two entries within the two queues are only meaningful together - then create a class that wraps around one A and one B - and put such objects into a single queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM