How can I make ThreadPoolExecutor command wait if there's too much data it needs to work on?

后端 未结 4 1411
我在风中等你
我在风中等你 2020-12-14 04:45

I am getting data from a queue server and I need to process it and send an acknowledgement. Something like this:

while (true) {
    queueserver.get.data
             


        
4条回答
  •  有刺的猬
    2020-12-14 04:54

    If you want the acknowledgment when the worker starts working on the task, you can make a custom ThreadFactory that sends the acknowledgment from the thread before doing the actual work. OR you can override beforeExecute of a ThreadPoolExecutor.

    If you want the acknowledgment when a new worker is freed up for a new task, I think you can initialize a ThreadPoolExecutor with a SynchronousQueue and a ThreadPoolExecutor.CallerRunsPolicy, or with your own policy where the caller blocks.

提交回复
热议问题