jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Thread running withing a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread running withing a servlet" Watch "Thread running withing a servlet" New topic
Author

Thread running withing a servlet

fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
Hello!
I tried the follwoing simplest code of running a thread in a servlet
but it does not seem to.




Apache displays a blank page as if nothing happens.
I just want my doGet method to provide a seperate response for each incoming request.
Could you please help me?
Waiting for reply.......
Thank you.
Fedai

[Edit by Dave: Code tags added]
[ March 26, 2006: Message edited by: David O'Meara ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Please use code tags.

I just want my doGet method to provide a seperate response for each incoming request.
The Servlet container will already do this for you. It manages calling your Servlet with a separate thread for each request (and response).
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
As david has suggested Servlets are inherently multi-threaded. It will use a pool of threads to handle incoming requests. This is why Servlets are more efficient that CGI scripts. It is also not recommended if not forbidden to start new threads within Servlets like timer threads etc.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Eduardo Dela Rosa
Greenhorn

Joined: Apr 15, 2004
Posts: 25
Hi fedai,

You wouldn't see the anything on the browser (as a response to your request).

That's because you've delegated the responsibility of delivering the output stream to another thread (i.e., not the Servlet's own thread).

And because you've dealt with using another thread, you cannot be certain
when that new thread will write the output to the browser; it may sleep for
a moment or will have the chance to run immediately as that with the Servlet's thread (which normally has the higher priority among others within the Servlet Context, a.k.a., servlet container.

BUT if you really want to see the result, try to click on the *refresh* button and you should be able to see the output this time.

Cheers.

Originally posted by fedai gandjaliyev:
Hello!
I tried the follwoing simplest code of running a thread in a servlet
but it does not seem to.




Apache displays a blank page as if nothing happens.
I just want my doGet method to provide a seperate response for each incoming request.
Could you please help me?
Waiting for reply.......
Thank you.
Fedai

[Edit by Dave: Code tags added]

[ March 26, 2006: Message edited by: David O'Meara ]


Eduardo dela Rosa<br />SCJP 1.4
fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
I see everything now!
Thank you very much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread running withing a servlet