aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes manning's question about thread safe 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 "manning Watch "manning New topic
Author

manning's question about thread safe

air lulu
Ranch Hand

Joined: Jan 11, 2005
Posts: 42
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
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.

So the answer are B and E.

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
but i m so surprised to see that such statements...

private static Hashtable staticHash = new StringBuffer();
private Hashtable instanceHash = new StringBuffer();


is these above statement possible ?.. How can you but stringbuffer object into hashtable object reference...

pls clear my


Thanks and Regards, Amit Taneja
Dhanesh Kumar
Greenhorn

Joined: May 06, 2005
Posts: 17
private static Hashtable staticHash = new StringBuffer();
private Hashtable instanceHash = new StringBuffer();

i tried to compile above tow line. i got error like following


C:\Test.java:7: Incompatible type for new. Can't convert java.lang.StringBuffer to java.util.Hashtable.
private static Hashtable staticHash = new StringBuffer();
^
C:\Test.java:8: Incompatible type for new. Can't convert java.lang.StringBuffer to java.util.Hashtable.
private Hashtable instanceHash = new StringBuffer();
^
2 errors

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
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
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).


Thanks,<br />Satish<br /> <br />SCJP 1.4 - 98%
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

air lulu,

I would like to mention that SingleThreadModel is not part of SCWCD 1.4.
Are you sure you are using the right book ? (Second Edition)


[My Blog]
All roads lead to JavaRanch
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

The SingleThread Model is not in current Exam.

There is Errata on this question at manning site. The corrections are

Page 171 in Question 3, 5th and 6th line:

Current:

private static Hashtable statichash = new StringBuffer();
private Hashtable instancehash = new StringBuffer();

Change To:

private static Hashtable statichash = new Hashtable();
private Hashtable instancehash = new Hashtable();

I think the question is intends on which type of variables are threadsafe in Single thread model, not on hashtables.

Thanks
air lulu
Ranch Hand

Joined: Jan 11, 2005
Posts: 42
thx all!!!

TO Satou kurinosuke :
u are right.
i am not using the second edition.
can u tell me where to find or download the second edition of manning SCWCD?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
Thanks to satish for nice explaination
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: manning's question about thread safe