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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 39535
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    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


Java Resources at www.wbrogden.com
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
 
Similar Threads
Synchronizing between two threads, one implements Runnable the other extends Thread
Some clarification on lock, wait in a synch block of code - please help
Thread
FileReader TimeOut help
Java 1.6.0.20 broke my SwingWorker threads!