Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Will you ever really need to spawn separate thread if you are writing servlets?

 
Ipsita Naravane
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the thing. Let's say I have a servlet that aacepts some query from the user and runs it against the database, and if I wanted to allow multiple queries to run simultaneously, I don't need to explicitly spawn a new thread right? When a request is sent to the app server, it will automatically spawn a new thread correct? So all I need to be sure of is that the data consistency issues etc. and synchronization is properly handled in my code. Am I correct in assuming this?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Each request gets its own Thread - typically a pool of Threads is managed by the servlet container.
2. Servlets must be designed around the request/response cycle. Client applications - typically browsers with humans looking at them - have an expectation that they will get a response within a certain amount of time.
3. IF your queries can execute within the expected time you don't need to do anything special. The request Thread can handle it.
4. IF your request sets off a process that will take a long time (relative to what the client expects) you WILL have to do something special that may or may not involve spawning a new Thread that works while the original request Thread sends some reassuring message back to the user.
5. This sort of thing gets discussed a LOT around here so browse around in the historical forum threads.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic