I'm looking for a way to limit access to a method in java to no more than once every X seconds. Here's my situation :
I want to run this code in parallel with multiple threads :
private MyService service;
public void run() {
// Send request to remote service
InputStream response = service.executeRequest();
// Process response
... some more code
}
The executeRequest() method sends an http request to a remote server (which isn't mine, I have no access to its implementation) and waits for the response from the server. It then does some processing of the data. I would like to have many threads run this in parallel. My problem is that the remote server will crash if too many requests are sent simultaneously. So I want some way of making sure that the executeRequest() method will never be called more than once every second.
Do you know how I could do that in java ? Thanks