views:

45

answers:

2

Hi to all! I'm facing the following problem. i have a servlet that serves a client request with a video clip. But this video clip is a product of another thread (transcoder). If the clip is not ready to be downloaded because the transcoder thread haven't finished its job, the client request fails! Any suggestions about how to deal with this case? how can i halt the response of the servlet until the transcoded clip is ready by the thread?

Thanks in advance! Antonis

+2  A: 

You can delay the execution of the servlet with the regular means (sleep, wait, join, Future#get).

In your case, it sounds like you want to join the Thread (or get the Future) that transcodes the video.

However, you should really only do this if the time you need to block is just a few seconds. Otherwise, the client browser might time out the request, or the user experience is just bad. If the transcoding work takes longer, consider outputting something like a progress bar, that keeps polling the server until the task is finished and only then tries to load the video clip.

Thilo
+2  A: 

The most straight-forward thing to do here would be to use a Future. Submit request to the transcoder, and let it return a Future immediately. Then the HTTP thread can block on this future calling get until the video is ready.

Joining doesn't sound like a good option to me. Thread#join blocks until the target thread terminates, but whether a thread terminates after doing a job is implementation detail. For example, if the transcoder would use a cached thread pool, the app. breaks.

Enno Shioji
Great answers from both of you. I' ll try to see which one fits my needs. Thanks both of you
Antonis
Again, only do this if it blocks for a few seconds. For longer tasks, you cannot just keep the servlet and the user's browser hanging there.
Thilo