• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about threading in servlet.

 
jamil lusa
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.
 
Tim Moores
Bartender
Posts: 2749
38
  • 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
Bartender
Posts: 2749
38
  • 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.
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • 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
Bartender
Posts: 2749
38
  • 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
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to correct me Tim, I just forgot it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic