aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Explanation required about a question from HeadFirst book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Explanation required about a question from HeadFirst book" Watch "Explanation required about a question from HeadFirst book" New topic
Author

Explanation required about a question from HeadFirst book

Mark heningen
Greenhorn

Joined: Mar 25, 2010
Posts: 25
I am referring to question 13 chapter number 5, page 216 from HeadFirst book 2nd ed.
What is the recommended way to deal with the servlets and thread safety?
A. Write the servlet code to extend ThreadSafeServlet
B. Have the servlet implement SingleThreadModel
C. Log all servlet method calls
D. Use local variables exlusivley,and if you have to use instance variables, synchronize access to them
Correct answer is D.
But I think B should also be there along with D.

I know authors mentioned that SingleThreadModel is deprecated in 2.4 and is not recommended to use. But we don’t know about SUN or Exam setters.
Your ideas will be appreciated? May be if someone knows a VALID SUN link, which says that deprecated stuffs are considered as wrong answers?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

SingleThreadModel is interface which Ensures that servlets handle only one request at a time. This interface has no methods.

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 synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.

Note that SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. This interface is deprecated in Servlet API version 2.4.

For more details look Here


Life is easy because we write the source code.....
Mark heningen
Greenhorn

Joined: Mar 25, 2010
Posts: 25
@Pramod:Please look at MY question(s) again.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

What is the recommended way to deal with the servlets and thread safety?
A. Write the servlet code to extend ThreadSafeServlet
B. Have the servlet implement SingleThreadModel
C. Log all servlet method calls
D. Use local variables exlusivley,and if you have to use instance variables, synchronize access to them
Correct answer is D.
But I think B should also be there along with D.


Here B is not correct beccause SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. (SingleThreadModel hits the performance also).

Mark heningen
Greenhorn

Joined: Mar 25, 2010
Posts: 25
I am looking for this:
"I know authors mentioned that SingleThreadModel is deprecated in 2.4 and is not recommended to use. But we don’t know about SUN or Exam setters.
Your ideas will be appreciated? May be if someone knows a VALID SUN link, which says that deprecated stuffs are considered as wrong answers? "
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Anything deprecated is not recommended for use. This statement is valid for everyone.


[My Blog]
All roads lead to JavaRanch
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Mark heningen wrote:I am referring to question 13 chapter number 5, page 216 from HeadFirst book 2nd ed.
What is the recommended way to deal with the servlets and thread safety?
A. Write the servlet code to extend ThreadSafeServlet
B. Have the servlet implement SingleThreadModel
C. Log all servlet method calls
D. Use local variables exlusivley,and if you have to use instance variables, synchronize access to them
Correct answer is D.
But I think B should also be there along with D.

I know authors mentioned that SingleThreadModel is deprecated in 2.4 and is not recommended to use. But we don’t know about SUN or Exam setters.
Your ideas will be appreciated? May be if someone knows a VALID SUN link, which says that deprecated stuffs are considered as wrong answers?


I thought as you too. Until I saw this answer. Just remember that SingleThreadModel is bad!
So if the question asks if you must use it, just say no and you will be happy.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explanation required about a question from HeadFirst book