File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Long running task in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Long running task in servlet" Watch "Long running task in servlet" New topic

Long running task in servlet

Ris Yagnik

Joined: Feb 19, 2008
Posts: 11

I want to run some background job without blocking user's browser which I started in Thread.

here is my code snippet :

calling from controller :

Now my problem is that thread is not sleeping and it blocks user's browser for ever ...

any help will be appriciated..


[BSouther: Added UBB CODE tags]
[ March 05, 2008: Message edited by: Ben Souther ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
It looks as if this code will not terminate until "replaceTaskFinished" is set to true (which supposedly happens in replaceTaskThread). So you haven't really an independent thread here.

Also, if "replaceTaskFinished" is an instance variable, then this code is not thread-safe. Any write access to instance variables needs to be synchronized. But, as pointed out above, the code should not wait for it to become true to begin with.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13024
Hardly surprising since (as near as I can tell) you are holding on to the thread that is handling the request that started the whole thing. You should let that request terminate normally with something like a "working" HTML page and find another way to update the user. There have been a lot of discussions of this sort of thing on the ranch so do some searches.

Ris Yagnik

Joined: Feb 19, 2008
Posts: 11
Thanks for all the replies...

I got what you both are saying ...How about setting ReplaceTask Thread as daemon thread will that work ?


Do you think daemon thread will garbage collected by web container any idea on performance how will it affect ?
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

On my site, I have a simple demo app that does something similar.

Look for LongRunningProcess.
It might be enough to get you going in the right direction.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ris Yagnik

Joined: Feb 19, 2008
Posts: 11
Thanks for all the help...It helped a lot

Don't get me started about those stupid light bulbs.
subject: Long running task in servlet
It's not a secret anymore!