This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Usage of Threads in Web applications Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Usage of Threads in Web applications" Watch "Usage of Threads in Web applications" New topic
Author

Usage of Threads in Web applications

Avianu Sud
Ranch Hand

Joined: Jan 20, 2002
Posts: 55
I have NO instance variables in HttpServlet classes, and all calls to helpers are either local instance variables, or static calls for data that is displayed on JSP pages.

Example:

class ShoppingCart extends HttpServlet {

public void execute() {
int selectedBook = request.getAttribute("bookId");
Delegate delegate = new Delegate();
Book book = delegate.getBookDetail(selectedBook);
ArrayList authors = delegate.getAuthorDetails(selectedBook);

request.setAttribute("bookDetail", book);
request.setAttribute("authorDetail", authors);

// show book detail or forward to jsp page...
}
}


I have not seen any need to make any code ThreadSafe in this case.
1) Is there any situation that will fail the application?
2) How much are Threads used in High transaction Web based applications? (Extensive/Minimal/As needed?)

Please share your expertise,
- Avi
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It looks like you're following safe practices well. Unless the delegate could give the same Book instance to two servlets at the same time, which is probably not the case.

The container uses threads like crazy, but application developers rarely do in servlets. You'll see conversations here about running a thread that does some action periodically, like an hourly database update or something. I wouldn't expect more than a handful of those ever.

It's also possible to start a thread to do some work for the user and return immediately. It's tricky to get the results back to the user when you do that and it's not common.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Avianu Sud
Ranch Hand

Joined: Jan 20, 2002
Posts: 55
James,

Very Insightful response. Thank You.

PS. I ususally rely on Timers and Messaging for batch and external system processing(which also handle Threads internally).

I just get a bit concerned when technologists keep making Threads as a Primary issue in Web Based Applications.

My perspective: Threads are critical, but mostly handled by Containers for us.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Usage of Threads in Web applications
 
Similar Threads
Head First BeerSelect problems
Why so much emphasis on Business Delegate or Intercepting Filter
thread safe
Servlets JSP and Design FAQS
Servlets and Threads