Hello, I have such a problem: it is required to simultaneously send https requests to load the host. Request is quite complex and must be "assembled" dynamically for dispatch. I thought of doing it this way: there is a set of threads that generates requests, and adds them into some kind of structure (suggested that ArrayBlockingQueue ) and there is a set that pulls them out and sends (there is a separate HTTP client in the flows of this set). But then I thought about it - once the structure is total, blocking, then 2 threads at the same time will not be able to pull the request out of it, therefore, they will not be able to send simultaneously, even if they are physically executed simultaneously on a 2-nuclear stone. Can you please advise some more productive approach. Thank. Question avp: because the generation of requests takes more time than sending, so while there is something generated nothing is sent and there is no load on the host. Ideally, it is necessary that 3-4 generator streams should work on 1 sender

  • The second way I see is that both sending and generation should be carried out in the same stream, but this is not convenient, because the generation can take a long time + the generation will again have to contact the same data stores used for making the request , and this is synchronization again. - Vladimir
  • The third way is to generate requests in a separate pool (ExecutorCompletionService) - and in the sending pool of requests to take them through the first “ready” Future, what do you say? - Vladimir
  • The 4th thing that occurred to me was to generate a request in one thread, create a Task into which to transfer this request, and send a task to another pool, in Task to get from some object pool (which is not synchronized, I don’t know how to implement it) ) client and send the request through it, after which the task is completed. - Vladimir
  • one
    I do not understand what is the connection between pull out of the blocking structure and send . Indeed, the threads will take out the request one by one (synchronizing to ArrayBlockingQueue), and then what does it take for them to send it without blocking each other? - avp
  • If possible, I want to avoid any kind of flow dependency between each other, to ensure 100% parallelism. It is necessary to provide parallel , constant load on the host - Vladimir

1 answer 1

Well, if you are sure that sending from the same stream as generating data is impossible, then make several sending streams. All of them will take data from your * BlockingQueue and fall asleep when there are no tasks in it.

  • You misunderstood me :) I already have several sending streams, several generating ones. Whenever possible I want to make sure that sending threads do not block each other when trying to get a request from * BlockingQueue - Vladimir
  • This is most likely impossible. And do not. The blocking time in this place is very short and the content is unlikely here. So do not waste time on this garbage. - cy6erGn0m
  • Ok, I just wanted to know the opinion of more experienced people in this business. How would they implement such a task ... I thought up another way - for each sender thread - its own request queue, several generator streams are added to each request queue. What do you think ? - Vladimir
  • This will lead to poor load balancing between sending threads and, as a result, can lead to the situation that one thread has a huge queue and the rest are idle. In this case, the loss will be much more significant than the loss of synchronization. Manual rebalancing is a boring task and, again, not free. And definitely not lock-free. - cy6erGn0m
  • one
    And you do not get away from synchronization in any case. Only if you send a request from the stream that generates the request. Perhaps this is actually the best option. Its main advantage is simplicity and lack of synchronization (almost, since it is necessary to be able to handle timeouts). - cy6erGn0m