• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

What is thread safe?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes your answers are correct. I verified this with other websites and mock exams.
 
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Francois Roland
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget that class and session objects are also NOT thread safe.
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Bernhard Slominski
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic