• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts in SingleThreadModel

 
naresh govindaswmay
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • 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.
 
James Mark
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • 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
 
Rhett Wang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E is not right??!!
Why?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources.
 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic