• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

doubts in SingleThreadModel

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Which statement is TRUE regarding the following code?
Please select one correct answer.
import javax.servlet.*;
import javax.servlet.http.*;

public class MyHttpServlet extends HttpServlet
implements SingleThreadModel {

StringBuffer bufferOne = new StringBuffer(); //1

static StringBuffer bufferTwo = new StringBuffer(); //2

protected void doGet(HttpServletRequest req, //3
HttpServletResponse res) throws java.io.IOException{

HttpSession session = req.getSession(); //4

res.setContentType("text/html");
java.io.PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>This is my servlet!</title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
out.close();
}

A.Variable bufferOne at //1 is NOT thread-safe.
B.Variable bufferTwo at //2 is NOT thread-safe
C.Both A and B
D.Variable req at //3 is NOT thread-safe
E.Variable session at //4 is NOT thread-safe
F.Both D and E

I have selected c is the answer, because class variable are not thread safe.But they tole "B" is the rite answer. anybody please clear this doubt.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With the single thread model you know for sure that only one thread per instance of the servlet will run. This means that instance variables are thread safe.

You should not use the single thread model because it's depreciated and considered evil.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HttpSession session = req.getSession(); //4
session is a local variable...
but session Attributes are not thread safe.....
I think session is also not thread safe beacuse the session object can be
accessed by different clients.
Please clarify me
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
E is not right??!!
Why?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please QuoteYourSources.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

as it is a single thread model all the variables are thread safe but even though HttpSession session = req.getSession(); session variable is also thread safe but the data with in the session scope are not since when some other thread executing code paralley it can get same session in that case it can still modify the values in session scope.

Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic