File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Regarding SingleThreadModel from J@Whiz 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 "Regarding SingleThreadModel from J@Whiz" Watch "Regarding SingleThreadModel from J@Whiz" New topic
Author

Regarding SingleThreadModel from J@Whiz

Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
Hi,
If I have a session variable declared inside a service() of a servlet which implements SingleThreadModel,then isnt' that session variable thread-safe(as ALL LOCAL variables are)
viz
public class XYZ implements SingleThreadModel
{
int i=0;//NOT thread safe
public void service(HttpServletRequest req,HttpServletResponse res)
{
HttpSession sess=req.getSession();//Threadsafe or not???
}
}
The answer is given as session is not thread safe.Why it is a local varaible & must be thead safe,isnt it?
fazal
Greenhorn

Joined: Jul 15, 2002
Posts: 1
Hi there,
U seem to be a bit confused with the session variable. First of all, session object is created by the servlet container and not by you. When u use the statement "HttpSession sess=req.getSession();" u r actually trying to get a reference to an existing variable and not creating a new object.
Hope this clears ur doubt.
Happy Coding...
Fazal.
[ December 17, 2002: Message edited by: fazal ]
Hari babu
Ranch Hand

Joined: Jun 25, 2001
Posts: 208
Hi,
Your variable "int i" is thread safe. since u are implementing the single thread model interface.
Hari
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
There is only one copy of the instance variable per instance of the servlet, and as you implements SingleThreadModel, there is always only one thread that execeutes the methods of a servlet instance at a time. So instance variable are thread-safe while you're implementing SingleThreadModel.
Hope it helped you!


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Remember that instance variable are Thread-safe if you're implementing SingleThreadModel, but static variable are not thread-safe, even if you're implementing SingleThreadModel. This is because there exists only one copy of the static variable across all the instances. So even, when Multiple-threads access differents instance of your servlet, they are accessing the same static variable.

I hope it helps!!
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Originally posted by Vedhas Pitkar:
The answer is given as session is not thread safe.Why it is a local varaible & must be thead safe,isnt it?

Sorry I haven't read well your question, I let the message that I posted as it may be interesting for other people.
Session variable are not thread-safe. Session variable are accessible only to the threads belonging to that session. But they're may be multiple threads belonging to one session. For example a user may open multiple browser windows and send request. All the request will belong to the same session and they will be able to access the session attributes simultaneously.
I hope this one helped you really
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
There is a tutorial on sun site
Swanand Barve
Greenhorn

Joined: Dec 16, 2002
Posts: 1
Younes,
I think that if a user opens multiple browser windows, then these will share a session only if cookies are enabled. If not, url rewriting will be used and each browser will represent a different session.
Regards
Swanand
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
Originally posted by Swanand Barve:

I think that if a user opens multiple browser windows, then these will share a session only if cookies are enabled. If not, url rewriting will be used and each browser will represent a different session.

I don't think that the statement above is right. Anyone can correct me if I am wrong.
Regards,
Zheng
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
In fact, I'm not sure about it. I never thought about it . As far as I know, once you close your browser and open it again, you get a new sessionId. So, IMHO I think that you lose the session and I would agree with Swanand. Hopefully, there is some experts this week that could answer our questions
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
But the session variable is still not thread safe, since the client may use cookies
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
All agreed,but the session variable is INSIDE the service() & as such a LOCAL variable whose scope finishes as soon as the service() is exited.
I put my question clearly now:
Are any HttpSession or ServletContext variables declared inside any method(local vars) are thread-safe or not?Coz as instance variables they are NOT thread-safe.
Furthur comments,anyone?
bor
Greenhorn

Joined: Dec 26, 2002
Posts: 11
Dear Pitkar,
A servlet that implements SingleThreadModel only guarantees that only one thread in this servlet's instantce at a time will execute.
Any outside object that is referenced by this servlet instance is not thread-safe. It may be accessible at any time to those servlets instances.


bor
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
Dear friends and pitker,
As far as i know," when servlet implements Single thread model it is same as declaring service method of that servlet as Synchronized ".
So all the local variable in that Service method is Thread safe, So as per Servlet Specification 2.3, " Multiple Servlets executing request threads may hv active access to a single session object at the same time, So the session resources r not thread safe even if servlet implements Single thread Model ! "
Please see to page no: 52 (srv 7.7.1 of chapter 7), of Servlet specification (http://java.sun.com/products/servlet/download.html)


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

Joined: Feb 28, 2002
Posts: 81
Moreover, " Multiple Servlets executing request can simultaneously access and modify the session attributes of a single session object at the same time ! "
( so the bottom line for yr question is : " request object is not part of yr service method so request can not be Synchronized and so session object and even other session resources r not thread safe ! )

Hope i m clear enough !
Best regards, Dharmin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding SingleThreadModel from J@Whiz