• Post Reply Bookmark Topic Watch Topic
  • New Topic

using util class that create thread in servlet  RSS feed

 
zhiming jia
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it ok to call a utility class which creates threads to do its job in a servlet's doGet/doPost method? Any input appreciated.
 
Vishal Pai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should not be a problem. I have done this a couple of times.
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no case where a thread has to be created and used in a servlet.
Each servlet request has its "own" dedicated thread. This thread manages the entire cycle(doget/dopost) of a servlet.
Once the doget/dopost/service is complete, the request and response objects will no more be available. So cant use any of the objects which are of request and responce type.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not recommended but it's done all the time.
Make sure to use daemon threads.
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
Can you please give me instances where there is a requirement to create thread inside a servlet? and also how to create a thread as daemon?

Thanks
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about a situation where you want to kick off a long-running process and you don't want to bog down the user's browser waiting for it (it might even time-out anyway)? In that situation, maybe you're going to email the user when you're finished to let them know that the results are available for review or just that the operation is finished. As for creating daemon threads, it's simple...



The trick is what to do with this daemon thread. One idea might be...



In your servlet code, then, you would do something like...

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!