简体   繁体   中英

ExecutorService sleep all threads in thread pool

I have a problem with using ExecutorService , ideally I'm looking for a solution to sleep all threads inside of thread pool (playing with Adwords API, when pushing some ads I get rate exceeded error which I catch, and then sleep that thread, but other threads are not aware of that error and still work hitting API with rate exceeded response, which increases the waiting time until another request can be processed).

Is there any simple way to sleep all threads in the same time? I tried to find any solution, I have one in mind but I'm sure there is something similar that exist out there.

Thank you for any help and have a nice day.

There is no way for one thread to "sleep" another, and even if it was possible, it's not a good idea. Threads should cooperate .

If you're using an ExecutorService, that means, there is a queue of tasks. The simplest solution would be to limit the rate at which you put tasks into the queue.

A different solution would be to create a global "throttle" object that all of the tasks know about. It would have a method that each task can call to wait until it's OK to go. You'd want to put some thought into how that object was synchronized: A simple, global lock could be a significant bottleneck if the maximum rate that you are trying to achieve is high.

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