Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrox book question

 
Mark Ye
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 168, q number 9:
public class DateServlet extends ... implements SingleThreadedModel {
private HashMap map=new Map();
public void doGet(.....) {
map.put(....);
}
}
Which is true, select 2 answers:
a. map is thread-safe
b. map would be thread safe if its declared final
c. req is thread safe
d. map can never be thread safe
The book says the answer is ac, I have a problem with a, because isnt HashMap's put is not thread safe? So the correct answer should be cd, what do you think? Thanks in advance.
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you say that HashMap's put is not thread safe?
I think the answer is a and c.
Because if we implmement SingleThreadModel for our servlet, the countainer may create instance more than 1 for a servlet. So instance variable, map, is thread safe.
Correct me if i am wrong
thanks
daniel
 
boyet silverio
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
to further clarify, when a servlet implements SingleThreadModel, the servlet will be servicing one request at a time. That is, other requests for this servlet are held off while a request is currently being serviced. If necessary, the container may create several servlet objects to ensure this situation. This makes 'map' in the code above thread-safe.
If the servlet above did not implement SingleThreadModel, several threads (requests) may access the servlet simultaneously. This is ok if the value of map is the same regardless of any request and would not affect program results: thread-safety may not be an issue.
However if the value of map will depend on conditions specific to each request then thread-safety will become a problem. "map" will not be thread-safe. That is, a request might use a value of map generated by another request leading to erroneous results. To make "map" thread safe, synchronize it or as the code indicates, implement SingleThreadModel.
 
Varun Khanna
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark ,
You are right in a way !!! As if one have to choose between HashMap and HashTable, HashTable is synchronised and hence thread safe whereas HashMap don't provide this feature,
but the answer a,c are correct bcoz by implementing SingleThreadModel (servlet implementing this interface cannot service
requests concurrently. The specification guarantees that if a servlet implements this
interface, the container will not execute the service() method in more than one thread simultaneously.) the developer has ensured that this member variable won't be shared across multiple requests (unless been accessed using session or context, which is not mentioned here)
HTH
-Varun
 
Mark Ye
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the insightful replies, I guess in short, I could say that implementing SingleThreadModel makes HashMap.put() thread safe, so the answer is ac.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic