I have a main for-loop that sends out requests to an external system. The external system might take a few seconds or even minutes to respond back.
Also, if the number of requests reaches the MAX_REQUESTS, the current for-loop should SLEEP for a few seconds.
This is my scenario. Lets say the main for-loop goes to sleep say for 5 seconds because it has reached the MAX_REQUESTS. Then say a previous external requests comes back returns from callExternalSystem(). What will happen to the main for-loop Thread that is currently on the SLEEP state? Will it be interrupted and continue processing or continue to SLEEP?
for(...){
...
while(numRequestsProcessing > MAX_REQUESTS){
Thread.sleep(SLEEP_TIME);
}
...
callExternalSystem();
}
Thanks in advance.
Unless you’ve got some code to interrupt the sleeping thread, it will continue sleeping until the required time has elapsed. If you don’t want that to happen, you could possibly use
wait()/notify()instead ofsleep()so that another thread can notify the object that the main thread is sleeping on, in order to wake it up. That relies on there being another thread to notice that the external system has responded, of course – it’s not really clear how you’re getting responses back.EDIT: It sounds like really you should use a
Semaphore. Each time the main thread wants to issue a request, it acquires a permit. Each time there’s a response, that releases a permit. Then you just need to set it up with as many permits as you want concurrent requests. UsetryAcquireif you want to be able to specify a timeout in the main thread – but think about what you want to do if you already have as many requests outstanding as you’re really happy with.