Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes problem of thread safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "problem of thread safe" Watch "problem of thread safe" New topic
Author

problem of thread safe

dh chau
Greenhorn

Joined: Feb 01, 2003
Posts: 5
Is instance variable of Servlet implements SingleThreadModel thread safe ?
Also, is instance variable of Servlet not implements SingleThreadModel thread safe ?
Thank you !!
vivek sivakumar
Ranch Hand

Joined: Aug 09, 2001
Posts: 187
yes instant variable of single thread model is thread safe since multiple threads are not allowed to access a single servlet (but should not rely on it..prefer synchronizing methods ), but instant variable on multithread model is not safe .


SCJP, SCWD <br />A farmer learns more from a bad harvest than a good one.
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
Dear chau and vivek,
Instance variable of Servlet who implements SingleThreadModel r not Thread safe
And it is very obvious that Instance variable of Servlet who didn't implements SingleThreadModel r also not Thread safe.
The servlet who implements SingleThreadModel, makes synchronize access for only service() method. So local variable of Servlet becomes thread safe, if servlet implements SingleThreadModel but, Instance variable always remains unsafe whether servlet implements SingleThreadModel or not.
Hope this helps
best regards, Dharmin
[ February 02, 2003: Message edited by: Dharmin Desai ]

SCJP2 (93%),SCWCD(88%)<br />-------------------------------<br />Never under estimate yr self, just represent yr profile in proper manner.
Dominic Paquette
Ranch Hand

Joined: Dec 13, 2002
Posts: 64
Hi,
I think instance variables of a servlet implementing the SingleThreadModel are thread safe, since only one thread will be executing the service method at any given time.
Dominic
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
Dear Dominic,
The Instance variables r those which r declared at class level, the variables which r declared in the service method r known as " Local Variables "
See to my answers at previous thread:
http://www.coderanch.com/t/166326/java-Web-Component-SCWCD/certification/SingleThreadModel-Whiz
Best regards, Dharmin
Dominic Paquette
Ranch Hand

Joined: Dec 13, 2002
Posts: 64
Yes, but since you implement the SingleThreadModel, there will be only one instance of your servlet class processing the request. So, to me, I think it's ok to use instance variables. I'll check out your other answer, maybee I overlooked something.
Dominic
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
The servlet who implements SingleThreadModel, makes synchronize access for only service() method.

I don't think the above statement is always true.
Servlet API SRV.14.2.20 SingleThreadModel says --

If a servlet implements this interface, you are guaranteed that no two threads will
execute concurrently in the servlet’s service method. The servlet container can
make this guarantee by
1)synchronizing access to a single instance of the servlet, or
2)by maintaining a pool of servlet instances and dispatching each new request to a
free servlet.

Considering the two cases -
1)Synchronizing access to a single instance of the servlet- In this approach the container maintain a single instance of the Servlet. However, the container cannot send multiple requests to this instance of the servlet. What happen when multiple requests arrived, the container serialized these multiple requests in the manner that only one request is dispatched to the instance and all the new incoming requests are kept waiting until the first request finishes the execution. Not finding this a good approach, most servlet containers go for the second approach.
2)Instance Pooling -The container maintain a pool of the serlvet instances. For each incoming request, the container allocates a separate Servlet instance to service the request from this pool and after completion of the service the container return that instance to the pool.
When I surfed the net I found that most of the servlet containetrs like websphere/ Tomcat etc go for the second approach.
Thus in most of the servlet containers different instances of the servlets are created, making instance variables thread safe when SingleThreadModel is implemented.
This is what the Manning book also says & so I think for the exam sake instance variables are thread safe.
Practically speaking -Best Practice: Do Not Implement SingleThreadModel in Servlets
Refer -
http://www-900.ibm.com/developerWorks/cn/wsdd/library/bestpractices/do_not_implement_single_thread_model_in_servlets_eng.shtml


--Shweta<br />SCJP 1.4 <br />SCWCD
dh chau
Greenhorn

Joined: Feb 01, 2003
Posts: 5
Dear All,
Thank you for all of the replies.
I agreed with mathur, the method 2 - (maintaining a pool of servlet instances and dispatching each new request to a free servlet) is the common method.
P.S. mathur, very thanks for your detail explanation.
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
I dis agree with Shweta in 2 manners:
=> Specification
If a servlet implements this interface, you are guaranteed that no two threads will
execute concurrently in the servlet´┐Żs service method

there is no guarantee for Variables who r Instance variable.
=> Maintaining pool of Instances (Each request must hv an individual and unique instance) would help u to make Instance variable Thread safe. But synchronize access to service method will not help u to make Instance variable THERAD SAFE.
=> <b> Either maintaining Instances of Servlets or make a syanchronized access to service method is a container specific issue.</b> Specification has written that, it is not binding container for which approch to follow.

Shweta, i hv checked up at Manning book, Manning book has written the way u hv posted. But i m posting a same question for Book Authors at jdiscuss.com.
Best Regards, Dharmin
[ February 04, 2003: Message edited by: Dharmin Desai ]
[ February 04, 2003: Message edited by: Dharmin Desai ]
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Let us look at some scenarios and may be that will help.
1) service() method is not synchronized, the servlet is marked as non-distributable in the DD, servlet doesn't implement SingleThreadModel interface.
Only one instance of the servlet should be created by the container and the service method is not thread safe. Multiple threads can execute and stop at various lines in the service method.
2) service() method is not synchronized, servlet is non-distributable and servlet implements SingleThreadModel interface.
The container can serialize calls to service method or keep a pool of servlet objects. In any case, only one thread can execute the service() method at any given time.
3) service() method is synchronized, servlet is non-distributable and servlet doesn't implement SingleThreadModel interface
The container can create only one instance of the servlet object per VM. The container must serialize the requests to service method and should not create pool of servlet objects.
4) service() method is synchronized, servlet is distributable and servlet doesn't implement SingleThreadModel interface
The container can create only one instance of the servlet object per VM. The container must serialize the requests to service method and should not create pool of servlet objects.
It doesn't make sense to synchronize the service() method and implement SingleThreadModel interface.
Whether you have instance variables or not, you should consider your options to make sure only one thread can execute service() method at any given time. Having said that, which way is better? synchronize the service() method or implement SingleThreadModel?
You can sacrifice the performance and resource if you implement SingleThreadModel. The server may create objects for every request/session. Creating and destroying objects affects performance and eats resource.
On the other hand, you can sacrifice performance only if you synchronize the service() method.
Comments are welcome.
Kevin Mukhar
Ranch Hand

Joined: Nov 28, 2000
Posts: 83
Originally posted by Dharmin Desai:
[QB]I dis agree with Shweta in 2 manners:
=> Specification
there is no guarantee for Variables who r Instance variable.

Yes, there is. The specification requires that the container only allow a SINGLE thread to execute in any servlet that implements SingleThreadModel. This means that no other thread can execute in the servlet. Since only a single thread can execute in a servlet that implements SingleThreadModel, instance variables are thread-safe in a servlet that implements SingleThreadModel.

=> Maintaining pool of Instances (Each request must hv an individual and unique instance) would help u to make Instance variable Thread safe. But synchronize access to service method will not help u to make Instance variable THERAD SAFE.

Yes, it will. The only way for a servlet to process a request is for the container to call the service method. If the container synchronizes the call to the service method, only one thread can execute in the servlet at a time. Since only one thread can execute, only one thread has access to the instance variables, thus they are thread-safe. In terms of thread-safety, this is equivalant to putting a sychronized block around the variables.
Kevin Mukhar
Ranch Hand

Joined: Nov 28, 2000
Posts: 83
Originally posted by Sai Prasad:
Whether you have instance variables or not, you should consider your options to make sure only one thread can execute service() method at any given time. Having said that, which way is better? synchronize the service() method or implement SingleThreadModel?

Both are BAD! Neither approach is scalable, especially synchronizing service(). Further, implementing SingleThreadModel does NOT guarantee that the servlet is thread-safe (despite the claims of the spec). There are two situations where a SingleThreadModel servlet is not thread-safe: 1) the servlet uses Class (static) variables; 2) the servlet access an external resource.
The correct approach is to design a thread-safe servlet from the start. Here are some guidelines
1) Only use local (method) variables.
2) Only use instance variables for data that does not change
3) If you must use an instance variable, synchronize the smallest possible block of code to protect access to the variable. Do NOT synchronize service(), doPost(), or doGet().
4) If your servlet accesses an outside resource (for example, a file in the file system) protect access to the resource using a synchronized block.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Kevin,
Even if I follow all the 4 guidelines, there is a chance that multiple threads can wade through the service() method unless I declare the service() method synchronized or implement SingleThreadModel interface. Do you agree?
-- Sai
[ February 05, 2003: Message edited by: Sai Prasad ]
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
Thanks every body
Specially to Dominic, shweta and kevin.
I hv brushed up my fundamentals from SCJP book.

- Dharmin
Kevin Mukhar
Ranch Hand

Joined: Nov 28, 2000
Posts: 83
Originally posted by Sai Prasad:
Even if I follow all the 4 guidelines, there is a chance that multiple threads can wade through the service() method unless I declare the service() method synchronized or implement SingleThreadModel interface. Do you agree?
[ February 05, 2003: Message edited by: Sai Prasad ]

Yes, absolutely! That is the whole point of servers and web servers and servlets and JSPs. Mutltiple clients can and will make concurrent requests to your web application. Your servlet must be prepared to handle multiple concurrent requests, just like the web server is prepared to handle multiple concurrent requests. If you dig into Apache or Tomcat or whatever web server and servlet container you use, you will probably find some discussion of thread pools and handling concurrent requests. Web servers do not process a single request at a time, they prepare for and do handle multiple concurrent requests.
Think of a popular web service like Google or Amazon or Yahoo or any ecommerce site. These sites, especially the very popular ones, must be able to handle thousands of transactions a minute (One day during december, I tracked amazon for a single minute. During the minute I chose, amazon claimed to have processed about 1700 transactions.) Now obviously, they could have a couple of thoudand servers, so that no request every conflicts with any other request, and whatever software they use for server side processing never has to handle more than a single thread at a time. But the better solution is not to synchronize service() or doPost() or doGet(), it is not to use SingleThreadModel, the better solution is to make the servlet thread-safe. The way you do that is with the four steps I outlined above.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem of thread safe