aspose file tools*
The moose likes Servlets and the fly likes Same Servlet, Multiple Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Same Servlet, Multiple Threads" Watch "Same Servlet, Multiple Threads" New topic
Author

Same Servlet, Multiple Threads

Julio Gutierrez
Greenhorn

Joined: Oct 26, 2013
Posts: 3
Hi There!

I'm doing some silly exercises in order to get ready for OCPWCD, anyways according to the theory, the same servlet, should run on different threads on per request, so i did a simple example:



and then i just call the servlet multiple times, and bam! every request runs one after the other on the same thread.

if you want to see it on action you can download the entire repo git@github.com:bubuntux/OCPWCD.git (is pretty small), then mvn tomcat6:run, then open http://localhost:8080/chapter4/index.html and click on SameServletMultipleThreads link.

Perhaps i'm missing a tomcat config? not really sure why this servlet is not running on different threads at the same time.

any idea??

thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

The servlet container handles running the threads. You should not be trying to create your own threads in the servlet.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Julio Gutierrez
Greenhorn

Joined: Oct 26, 2013
Posts: 3
Bear Bibeault wrote:The servlet container handles running the threads. You should not be trying to create your own threads in the servlet.


i'm not creating the thread in the servlet, take a look at the class, i'm just printing out the used thread
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Didn't look at the code -- just your post. You should post your code here. Please be sure to UseCodeTags.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

Julio, welcome to CodeRanch.

Are you sure multiple requests are indeed being fired simultaneously to that servlet? This thread reminds me of one other similar discussion where it turned out to be a browser issue, take a look at this one http://www.coderanch.com/t/476839/Servlets/java/multiple-requests-thread-serving

[My Blog] [JavaRanch Journal]
Julio Gutierrez
Greenhorn

Joined: Oct 26, 2013
Posts: 3
Jaikiran, thanks a lot

Taking a look at the discussion you mentioned, is the same issue, but i don't really think to the problem is firefox, i'm using chrome and seems like if you sent the exact same request, the container reuse the request and process it one after the other or something like that.

So anyways in order to actually see the same servlet running on diferent threads at the same time i changed the following:



to:



and bam! it works as charm.

Thanks everyone!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

It isn't the container which does that, it's the client (in this case the browser). You can enable "Developer Tools" for Chrome or Firebug for Firefox and verify whether or not the requests are sent simultaneously or sequentially by the browser.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Same Servlet, Multiple Threads