aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes What is thread safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "What is thread safe?" Watch "What is thread safe?" New topic
Author

What is thread safe?

Bernhard Slominski
Greenhorn

Joined: Nov 03, 2004
Posts: 10
Hi,

in HF Servlets and JSP Page 203 there is the question:
Which things are NOT thread safe:
1 Session scoped attributes
2 request scoped attributes
3 Instance variables in the servlet
4 local variables in service methods
5 static variables in the servlet

I don't find an answer for that question in the book.
I have a bit problems in understanding if the insstance/local variables of the servlet are thread safe.
So can you help me?

My guesses:
1 Session scoped attributes - not thread safe
2 request scoped attributes - thread safe
3 Instance variables in the servlet - not thread safe
4 local variables in service methods - thread safe (or do I have to synchronize on the request??)
5 static variables in the servlet - not thread safe

Thanks

Bernhard
srinath anand
Ranch Hand

Joined: Nov 20, 2001
Posts: 30
Yes your answers are correct. I verified this with other websites and mock exams.


SCJP2,SCWCD 1.4
Francois Roland
Ranch Hand

Joined: Jul 24, 2003
Posts: 34

You are almost right.

The only thing I would add is the fact that your instance variables can be considered thread-safe if your servlet implements the SingleThreadModel interface.


--
Brown belt on KnowledgeBlackBelt.com.
Francois Roland
Ranch Hand

Joined: Jul 24, 2003
Posts: 34

Oups...

This interface is now deprected "As of Java Servlet API 2.4, with no direct replacement." (as said in the sun javadoc).

So, forget my former answer if you work with Servlet 2.4+ specification.
Bernhard Slominski
Greenhorn

Joined: Nov 03, 2004
Posts: 10
Thanks for you answers!
The page number was not 203, but 201!
I just checked it again, and the answer is actually on page 202.
So I think I got it now.
Locals variables are thread safe, instance variables are not!
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
Don't forget that class and session objects are also NOT thread safe.


SCJP 1.4 SCWCD 1.4
alzamabar
Ranch Hand

Joined: Jul 24, 2002
Posts: 379
Also, the request and response objects are not guaranteed to be thread safe, even if you're implementing SingleThreadModel or isThreadSafe=false (from within your JSP).


Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
Bernhard Slominski
Greenhorn

Joined: Nov 03, 2004
Posts: 10
the request and response objects are not guaranteed to be thread safe

Marco, can you tell me exactly what you mean by that.
Request scoped attributes are thread safe right!
But the request object itself is not??
What does that mean for me, do I have put some of my code in a sychronized block and lock on the request object?
Can you give me an example what might go wrong?
alzamabar
Ranch Hand

Joined: Jul 24, 2002
Posts: 379
Originally posted by Bernhard Slominski:

Marco, can you tell me exactly what you mean by that.
Request scoped attributes are thread safe right!
But the request object itself is not??
What does that mean for me, do I have put some of my code in a sychronized block and lock on the request object?
Can you give me an example what might go wrong?


You are right when you say that request objects (in few words the variables local to your doXxx() method) are thread safe but the exam that also I've taken had questions like:

Which ones amongst those objects are thread safe in a servlet which implements the SingleThreadModel interface?

1) [...] a local variable
2) A static variable
3) An instance variable
4) A Request object
5) A Response object

Well, the exam (the one by Sun) gave 4 and 5 as not guaranteed by the specs.
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
There could be two interprations of "are the request and response object thread safe". One point of view is yes, they are, presuming that you don't create a thread to perform some other action within your application. Now if you take the point of view, no they are not thread safe because you can create another thread... then the scope of the threading issues becomes huge.

I would have answered this with, the request and response objects are thread safe. I believe in the HFSJ it says the request and response objects are thread safe.

From the spec, page 28:

SRV.2.3.3.3 Thread Safety
Implementations of the request and response objects are not guaranteed to be thread
safe. This means that they should only be used within the scope of the request handling
thread.
References to the request and response objects should not be given to objects
executing in other threads as the resulting behavior may be nondeterministic. If
the thread created by the application uses the container-managed objects, such as
the request or response object, those objects must be accessed only within the
servlet�s service life cycle and such thread itself should have a life cycle within
the life cycle of the servlet�s service method because accessing those objects
after the service method ends may cause undeterministic problems. Be aware
that the request and response objects are not thread safe. If those objects were
accessed in the multiple threads, the access should be synchronized or be done
through the wrapper to add the thread safety, for instance, synchronizing the call
of the methods to access the request attribute, or using a local output stream for
the response object within a thread.
[ March 02, 2005: Message edited by: Colin Fletcher ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is thread safe?
 
Similar Threads
what r thread safe variables
Thread safety and cookie age question...
Which among the servlet objects are synchronized
Sharpen your pencil P:201 answers
Thread Safety??