• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Same Servlet, Multiple Threads

 
Julio Gutierrez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet container handles running the threads. You should not be trying to create your own threads in the servlet.
 
Julio Gutierrez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't look at the code -- just your post. You should post your code here. Please be sure to UseCodeTags.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Julio Gutierrez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic