3. Consider the following servlet code: public class TestServlet extends HttpServlet implements SingleThreadModel { private static Hashtable staticHash = new StringBuffer(); private Hashtable instanceHash = new StringBuffer(); public void doGet(HttpServletRequest req, HttpServletResponse res) { StringBuffer sb = new StringBuffer() HttpSession session = request.getSession(); servletContext ctx =getServletContext(); // 1 } } Which of the following lines can be inserted at //1 so that the StringBuffer object referred to by the variable sb can only be accessed from a single thread at a time? (Select two) a staticHash.put("sb", sb); b instanceHash.put("sb", sb); c session.setAttribute("sb", sb); d ctx.setAttribute("sb", sb); e req.setAttribute("sb", sb);
which two? I select a,b and e
Narendra Dhande
Ranch Hand
Joined: Dec 04, 2004
Posts: 950
posted
0
Hi,
a staticHash.put("sb", sb); b instanceHash.put("sb", sb); c session.setAttribute("sb", sb); d ctx.setAttribute("sb", sb); e req.setAttribute("sb", sb);
The Option a is not correct. The static instance variables are not thread safe, though we implement single thread model. session and context attributes are not alway threadsafe. Instance variables are thread safe in single thread model.
i think the question may be wrongly asked.. pls anyone let me know that am i right or not
Dhanesh SCJP1.4/preparing SCWCD
Bhavik Patel
Ranch Hand
Joined: Jul 12, 2004
Posts: 155
posted
0
Hiii ..I am agree with the answers..But for option A i have doubt that Hashtable is synchronized in nature so will it mater if we have declared Static instance of that hashtable...
SCJP 1.4<br />SCWCD 1.4(91%)<br />Working on SCJD -Bodgitt & Scrapper Constructions...<br /> <br />"It takes 43 muscles to frown & 17 to smile but it doen't take any to just sit there with a dumb look on your face .. Keep Smiling "
Satish Ramasamy
Greenhorn
Joined: Feb 13, 2006
Posts: 15
posted
0
Amit, Dhanesh, Those statements are not possible. It could be a typo in the question.
Bhavik, Good point. But the question is NOT about staticHash being accessed from only one thread, it is about the variable sb being accessed from a single thread at a time. In this case even though Hashtable is synchronized by default, but the objects stored in it are not. i.e. you can have reference to the same object(sb) stored in the staticHash in two different threads at a time. For example, in strBuf = staticHash.get("sb"); statement, only one thread at a time can perform the get() operation, but once the operation is complete both the threads would have reference to the same StringBuffer object (strBuf).
Hence as per this question, option A is not correct (since more than one thread can access the same StringBuffer object).
There is no download for it. There was one for the First Edition with the CD, but there is no more CD, no more pdf. You'll have to buy it. But I'd recommend to buy Head First Servlet and JSP instead
Bhavik Patel
Ranch Hand
Joined: Jul 12, 2004
Posts: 155
posted
0
Thanks to satish for nice explaination
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.