Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

about threading in servlet.

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all, i am beginner in servlet. i have read the code below:



how these code determine currectly how many threads have been created by the servlet?

why argument "this" in hashtable is not refering to "this object" which is this servlet object but refering to thread?? anyone can explain the underlying mechanism or relationship between the servlet and threadS?

Thank very much for your kind reply.
 
Saloon Keeper
Posts: 5977
154
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why argument "this" in hashtable is not refering to "this object" which is this servlet object but refering to thread?


I don't understand the question - "this" refers to an object of type HolisticCounter. Nowhere in the code is information about the executing thread accessed.

Note that under almost all circumstances only a single instance of a servlet class is created, so "this" will be the same for each request. Also note that you will have concurrency issues with the "counter" and "hashTable" fields, as those are shared between all threads running the servlet. Access to those fields needs to be synchronized.
 
jamil lusa
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:

why argument "this" in hashtable is not refering to "this object" which is this servlet object but refering to thread?


I don't understand the question - "this" refers to an object of type HolisticCounter. Nowhere in the code is information about the executing thread accessed.

Note that under almost all circumstances only a single instance of a servlet class is created, so "this" will be the same for each request. Also note that you will have concurrency issues with the "counter" and "hashTable" fields, as those are shared between all threads running the servlet. Access to those fields needs to be synchronized.



hi thank your reply, i have read the tutorial from http://www.roseindia.net/servlets/HolisticCounterInServlet.shtml can you help me check the answer about the concurrent thread whether is correct or not?

Thanks!
 
Tim Moores
Saloon Keeper
Posts: 5977
154
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
roseindia.net is one of the crappiest tech web sites out there. I'm not surprised that code you got there doesn't work or is full of problems.
 
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Since Servlets is thread shared, you should not use local variables so that can't have a exact result which is expected. If you wanted to use a variable across all requests in a servlet you can go with one of the following ways.

1. Create a inner class to initiate all local variables, this should be called first inside the doGet/doPost method and created object to access. (Recommended way)
2. Make your servlet class implements SingleThreadModel. (Only a sinlge thread can be accessed your servlet, but performance is very low)
3. You can have synchronized methods or blocks. (Need to be very careful to avoid deadlock/performance).


Hope this helps you.
 
Tim Moores
Saloon Keeper
Posts: 5977
154
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kumaravadivel Subramani wrote:2. Make your servlet class implements SingleThreadModel.


Don't do that. In this day and age, you need to be able to write thread-safe server code, there's no way around that. Besides, using SingleThreadModel doesn't even solve all concurrency issues (which is the reason why it's deprecated).
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to correct me Tim, I just forgot it.
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!