简体   繁体   English

两个队列和不同的线程

[英]Two queues and different threads

I have 2 queues A and B. Main thread is responsible for filling the queues. 我有2个队列A和B.主线程负责填充队列。 There is a threadpool of 3 threads responsible for reading from queues. 有一个3个线程的线程池负责从队列中读取。 Both Queues are ArrayBlockingQueue. 两个队列都是ArrayBlockingQueue。 Lets say main thread is filling the queues in this way (A1,B1),(A2,B2),(A3,B3),(A4,B4) and so on. 假设主线程以这种方式填充队列(A1,B1),(A2,B2),(A3,B3),(A4,B4)等等。 A1 and B1 collectively makes a whole data. A1和B1共同制作整个数据。 Is it possible for a any thread to read data like (A3,B4) at any point. 任何线程都可以在任何时候读取像(A3,B4)这样的数据。 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. 例如,thread1应同时读取(A1,B1)和thread2(A2,B2),依此类推。

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. 如果两个队列中的两个条目唯一有意义的一起-然后创建环绕一项所述的和一个B类-并把这样的对象到一个单一的队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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