This week's book giveaway is in the Features new in Java 9 forum.
We're giving away four copies of Java 9 Revealed and have Kishori Sharan on-line!
See this thread for details.
Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Mock Question  RSS feed

 
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
Boost this thread!