简体   繁体   English

如何创建Java并发队列,从中我们可以在单个调用中阻塞多个元素?

[英]How to create Java concurrent queue from which we can blocking-take more than 1 element in single call?

Background: I need to send many small-size messages to WebSocket clients in asynchronous way. 背景:我需要以异步方式向WebSocket客户端发送许多小型消息。 Messages are usually sent in peak, so after some pause I need to send ~5000 messages fast. 消息通常以高峰发送,因此经过一些暂停后,我需要快速发送约5000条消息。 So the problem is: 所以问题是:

  • I don't want to start 5000 async's in single thread 我不想在单线程中启动5000 async
  • I don't want to loop "start async"-"wait for complete" 5000 times in serial 我不想连续循环“启动异步”-“等待完成” 5000次
  • I don't want to use 5000 threads, with single "start async"-"wait for complete" per thread 我不想使用5000个线程,每个线程只有一个“开始异步”-“等待完成”

The best way would be to group ~20 asyncs per thread, so I need very specific queue : 最好的方法是将每个线程约20个异步组,因此我需要非常具体的队列

  • lot of means concurrent push/poll in queue 大量意味着同时在队列中进行推/轮询
  • small-sized asynchronous means I want to poll in bundles, like 1 to 20 messages per queue take() (so I can start 1...20 async I/O and wait for completness in single thread) 小型异步意味着我想按捆绑方式轮询,例如每个队列有1到20条消息take() (因此我可以启动1 ... 20异步I / O并等待单线程完成)
  • immediately means that I dont want to wait until 20 messages will be polled, bundle-poll should be used only if queue has lot of messages. 立即意味着我不想等到将轮询20条消息时,仅当队列中有很多消息时才应使用bundle-poll。 Single message should be polled and sent immediately. 单个消息应被轮询并立即发送。

So basically: I need structure like queue that has blocking take(1 to X) waiting elements in single blocking call . 所以基本上: 我需要像队列这样的结构,它在单个阻塞调用中具有阻塞take(1至X)等待元素 Pseudocode: 伪代码:

[each of ~50 processing threads]:
messages = queue.blockingTake( max 10 or at least 1 if less than 10 available );
for each message: message.startAsync()
for each message: message.waitToComplete()
repeat

I wouldn't implement a Queue from scratch if it's not really necessary. 如果没有必要,我不会从头开始实施Queue。 A few ideas if you're interested: 有兴趣的一些建议:

Queue> if you have only 1 thread doing the offers. 队列>,如果您只有1个线程在执行商品。 If you have more, the collection has to be sync'd. 如果您还有更多内容,则必须同步该集合。 Like, one offerer peek()-s into the queue, sees that the last collection has too many elements so it creates a new one and offers it. 就像一个报价者peek()-s进入队列一样,它看到最后一个集合包含太多元素,因此它创建了一个新集合并提供了它。

or 要么

A number of running threads where the runnables take elements one by one from the queue. 许多运行线程,其中可运行对象从队列中一个接一个地获取元素。

or 要么

1 queue per sending thread, if you keep the queue references you can then add elements to each of them in a round robin fashion. 每个发送线程1个队列,如果保留队列引用,则可以循环方式向每个元素添加元素。

or 要么

subclass a BlockingQueue of your choice and create a "Collection take(int i)" method with a rewritten version of the normal take(). 将您选择的BlockingQueue子类化,并使用重写版的take()创建“ Collection take(int i)”方法。

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

相关问题 阻塞队列在 java 中不起作用,我可以放置比阻塞队列定义大小更多的元素 - Blocking queue not working in java, I can put more element than the defined size of blocking queue 我们如何从单个Java类文件创建一个类的多个实例? - How can we create more than one instance of a class from single Java class file? Java中的并发和阻塞队列 - Concurrent and Blocking Queue in Java 如何从阻塞队列中创建反应器通量? - How can I create reactor Flux from a blocking queue? 如何使用Spring Integration处理来自AWS SQS FiFo队列的10多个并发消息 - How to process more than 10 concurrent messages from an AWS SQS FiFo queue using Spring Integration 我们怎样才能在Spring中创建多个IOC容器? - How can we create more than one IOC Container in a Spring? 如何在 java 中的单个 package 中创建两个以上的类? - How to create more than two classes in a single package in java? 一个线程可以消耗多个队列吗? - Can a single thread consume more than one queue? 对于单生产者单一消费者方案,哪种Java阻塞队列最有效 - Which Java blocking queue is most efficient for single-producer single-consumer scenarios 如何创建一个基于java的应用程序,它将从网站获取输入 - How to create an java based application which will take input from website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM