Take the 2-minute tour ×

I have code called from a servlet that calls out to an external service. Of course, no guarantees how long the service will take to return a response. I need to ensure that no more than one call to this service executes at a time, but of course the servlet container can be running concurrent requests to the servlet. I want to guarantee that the priority of requests are processed single file, on a first come first server basis. So it is not enough that my call to the external servlet be synchronized because once the current call is finished there would be no guarantee as to which thread gets in to make the call next.

Any ideas?

share|improve this question
You don't have control over how long the requests take to get to your server, so you don't really know which one came first anyway. I wouldn't worry so much about the order of the requests, only that they are isolated from interacting with each other. –  corsiKa Mar 28 '11 at 17:46
imagine it takes 20 minutes to process first request. is it fair to let the nth request which came 1 second ago to execute before 1 to n-1 that may have come 20 minutes ago? You can have complete starvation in this way. (and yes 20 min is extreme and would pose other issues to be concerned with) –  harschware Mar 28 '11 at 18:14
Also, you are right, but the degree to which it is a problem I can mitigate by controlling access to this potentially long running resource, whereas the container's requests are less potentially long running (my app is the only one in the container). –  harschware Mar 28 '11 at 18:21
add comment

4 Answers

up vote 3 down vote accepted

You could use a fair Lock

Lock lock = new ReentrantLock(true);

This gives the lock in the order it was attempted.

share|improve this answer
see Chapter 13 of Java Concurrency in Practice, Bloch et al. –  harschware Mar 28 '11 at 20:46
thats was a good ans @peter.. but i think fair in lock work after the await stage.. means it the thread that have more time in waiting stage is activate first. –  aditya Dec 25 '12 at 12:24
@Anita Fair locking has more overhead and is slower. This is why it is not the default behaviour. ie you would only use it if you really needed it. –  Peter Lawrey Dec 25 '12 at 13:09
add comment

You can use single-threaded ExecutorService to submit Callables (which will perform actual request) and wait for Future value to become available.

share|improve this answer
I agree, but I'd like to underline that this pre-configured ExecutorService fits even better OP's needs (and the documentation states clearly that tasks are executed sequentially): download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/… –  akappa Mar 28 '11 at 18:46
add comment

Many utilities in java.util.concurrent are suitable for this situation, Semaphore with fairness setting is another choice

import java.util.concurrent.*;
Semaphore sem = new Semaphore (int n = HOW_MANY_PERMITS, boolean fairness = true);
share|improve this answer
add comment

Create a worker thread in your servlet and enqueue callouts there. Request handling would synchronize on adding a request to the queue, then just sit there waiting for the worker thread to report back.

share|improve this answer
add comment

Your Answer


