This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Thread safety Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Thread safety" Watch "Thread safety" New topic
Author

Thread safety

Rajesh Pathak
Ranch Hand

Joined: Aug 15, 2002
Posts: 56
Following is a question from the JR mock test itself.
Which of the following are considered ThreadSafe?
class variables, request attributes, context attributes, local variables.
My perception of thread safety is, that variable/attribute which cannot be acted upon by two threads simultaneously...
Can anyone throw some light on its answer and explanations.
Thanks.


Rajesh(Bangalore,India)<br />SCJP2, SCWCD, SCEA, IBM-XML, UML-OOAD, IBM-Enterprise Connectivity with J2EE.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
There is only one servlet instance per registered servlet. Normally, when concurrent requests are handled by the web server, threads are spawned which executes the service() method of the servlet. Instance variables are not thread safe, but local variables within the service() method are. The request object is also "localized" per thread.
According to the Servlet specs, when a servlet implements the SingleThreadModel interface (which is an empty "tag" interface) and concurrent requests are handled by the web server, the container may create a new servlet instance per request. It is possible that the container will pool these servlet instances. it is also possible that the container will serialize, i.e. queue, the requests per instance. It is possible that the container implementation is a combination of these.
Since there are now multiple servlet instances handling each request, the instance variables are now thread safe.
In either case, however, if any servlet static variables are defined, these will NOT be thread safe unless you explicitly make it so with custom code. The same argument goes for any external resources like context attributes.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
I think request attributes and local variables are thread safe.


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Rajesh Pathak
Ranch Hand

Joined: Aug 15, 2002
Posts: 56
Thanks for the info.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread safety
 
Similar Threads
Thread Safety??
which attribute scopes are thread-safe
Thread Safe Servlet
multiple threads for database
About SingleThreadModel