Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Long running task in servlet

 
Ris Yagnik
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ris Yagnik
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help...It helped a lot

Rishi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic