Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

manning's question about thread safe

 
air lulu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dhanesh Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Satish Ramasamy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to satish for nice explaination
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic