Search code examples
javamultithreadingspring-mvcconcurrencythread-sleep

What's the effect on a second request of calling Thread.currentThread().sleep(2000) in a Spring MVC request handler?


I need to wait for a condition in a Spring MVC request handler while I call a third party service to update some entities for a user.

The wait averages about 2 seconds.

I'm calling Thread.sleep to allow the remote call to complete and for the entities to be updated in the database:

Thread.currentThread().sleep(2000);         

After this, I retrieve the updated models from the database and display the view.

However, what will be the effect on parallel requests that arrive for processing at this controller/request handler?

Will parallel requests also experience a wait?

Or will they be spawned off into separate threads and so not be affected by the delay experienced by the current request?


Solution

  • What are doing may work sometimes, but it is not a reliable solution.

    The Java Future interface, along with a configured ExecutorService allows you to begin some operation and have one or more threads wait until the result is ready (or optionally until a certain amount of time has passed).

    You can find documentation for it here: http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html