Topic: Thread safe problem

  Print this page

1.Thread safe problem Copy to clipboard
Posted by: iany
Posted on: 2004-07-23 05:14

Given
11 public class Acct extends HttpServlet {
12  public StringBuffer foo = new StringBuffer("foo");
13  public void doPost(HttpServletRequest req,
      HttpServletResponse resp){
14  ServletConfig config = getServletConfig();
15  String param1 = config.getInitParameter("param1");
16  ServletContext context = getServletContext();
17  Object obj = context.getAttribute("OBJ");
}
}
Which two variables reference objects that are thread-safe?(Choose two)
A obj at line 17
B foo at line 12
C config at line 14
D param1 at line 15
E context in line 16
Answer C D
Why E is incorrect?

2.Re:Thread safe problem [Re: iany] Copy to clipboard
Posted by: yftsang2
Posted on: 2004-07-24 16:19

Because you can execute

getServletContext().getAttribute("OBJ");

and

getServletContext().setAttribute("OBJ");

concurrently in two threads, so ServletContext itself is not thread-safe. Correct me if I am wrong. Thanks.

3.Re:Thread safe problem [Re: iany] Copy to clipboard
Posted by: iany
Posted on: 2004-07-25 17:54

Thanks for your reply, but context is a variable and it's actually a local variable, i think anyone client get the same context value if they access the same application.

4.Re:Thread safe problem [Re: iany] Copy to clipboard
Posted by: javaguan2
Posted on: 2004-08-20 13:01

线程安全的概念是针对对象和实例的,与变量没有关系.故context变量是不是local variable无关紧要,它所指向的实例的范围也是线程安全所考虑的


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923