Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Spawning threads from Servlets?  RSS feed

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a process that will take a long time. Longer than the browser timeout feature. I was thinking that I could just spawn a thread to handle the process. Is that possible/acceptable? I don't recall ever seeing this done.

Is there a better way?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes this has been done.
People usually either have the application fire off an email when the process is done or return to a page that refreshes every n second to check the status of the process.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also make sure you understand the difference between daemon and non-daemon threads or you could make it difficult to stop and restart your app or container.
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

setDaemon
Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

So I should make it a daemon thread so that I can shut my application server (Tomcat) down whenever I want?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would depend on what the thread is doing.

If terminating a thread before it's done could cause data corruption (or a nuclear power plant to melt down), you might want your container to wait until the process is done.

If, on the other hand, the process is just building a reports or something less critical, you probably don't want your JVM to sit there and force you to wait when you're trying to bring down the app and/or container.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!