File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes about threading in servlet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "about threading in servlet." Watch "about threading in servlet." New topic
Author

about threading in servlet.

jamil lusa
Ranch Hand

Joined: Aug 18, 2011
Posts: 59
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Aug 18, 2011
Posts: 59
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jul 05, 2008
Posts: 166

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.


No pain, No gain.
OCJP 1.6
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jul 05, 2008
Posts: 166

Thanks to correct me Tim, I just forgot it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about threading in servlet.