Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Question

 
Sangita Hota
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.servlet.*;
import javax.servlet.http.*:
public class TestServlet extends HttpServlet implements SingleThreadModel {
private HashMap hm = new HashMap();
public void doGet(HttpServletRequest req, HttpServletResponse res) {
//lot of code.
}
}

Which of the following statements are correct?

a) 'hm' is thread safe
b) 'hm' is not thread safe
c) 'req' is thread safe
d) Making 'hm' static would make it thread safe.
e) 'hm' can be made thread safe only if it is made final.

Ansewrs given is a) and c)

How come a) is correct? In SingleThreadModel it may create more than one instance right??
 
shi lei
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when servlet implement STM interface,the servlet container will create more than one instance when some request receive.

so A is right.
 
mid
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are thread safe only when you implement SingleThreadmodel hence a is correct
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic