File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Long running task in servlet" Watch "Long running task in servlet" New topic
Author

Long running task in servlet

Ris Yagnik
Greenhorn

Joined: Feb 19, 2008
Posts: 11
Hi,

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..

Thanks,
Rishi


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

Joined: Mar 22, 2005
Posts: 42913
    
  68
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
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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.

Bill
Ris Yagnik
Greenhorn

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 ?

replaceTaskThread.setDaemon(true):
replaceTaskThread.start():

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

Joined: Dec 11, 2004
Posts: 13410

On my site, I have a simple demo app that does something similar.
http://simple.souther.us/not-so-simple.html

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
Greenhorn

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

Rishi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Long running task in servlet