Sun River
          Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ...
          posts - 78,comments - 0,trackbacks - 0
          Question 1:

          Consider the following servlet code:

          public class MyServlet extends HttpServlet
          {
          final static int i=0;
          public void doGet(HttpServletRequest req, HttpServletResponse res)
          {
          private HttpSession session=req.getSession();
          private ServletContext ctx=getServletContext();
          synchronized(ctx)
          {
          Object obj=ctx.getAttribute(); // code to alter obj
          }
          }
          }

          Which of the following variables in the above code are thread safe?

          Choices:

          • A. i
          • B. session
          • C. ctx
          • D. req
          • E. obj
          • F. res

          Correct choices:

          • A , C , D , and F

          Explanation:

          The static variable i is thread safe because it is final (cannot be modified), or else it would not have been safe. Request and response objects are scoped only for the lifetime of the request, so they are also thread safe. Session and ServletContext objects can be accessed from multiple threads while processing multiple requests, so they are not thread safe. However, in this case, the ServletContext object is synchronized, so it can be accessed only by one thread at a time. obj is not thread safe because even though the ServletContext object is synchronized, its attributes are not. They need to be synchronized separately. Hence choices B and E are incorrect and choices A, C, D and F are correct.

          Question 2:

          Which of the following statements are true?

          Choices:

          • A. Multiple instances may be created on a servlet implementing SingleThreadModel
          • B. No more than one instance is created for a servlet implementing SingleThreadModel
          • C. Even static variables in a SingleThreadModel servlet are thread safe
          • D. If no threading model is implemented, by default a servlet is executed in a multi-threaded model

          Correct choices:

          • A and D

          Explanation:

          When SingleThreadModel is implemented, the servlet container ensures that only one thread is executing the servlet's method at a time. So what will happen for multiple requests? In that case, the container may instantiate multiple instances of the servlet to handle multiple requests, so option A is correct and B is incorrect.

          If the SingleThreadModel interface is not implemented, a servlet uses the multi-threaded model (that is, multiple threads can access the methods of the servlet). Static variables can be accessed through multiple instances of the same class, so they are not always thread safe. Hence choices B and C are incorrect and choices A and D are correct.

          posted on 2006-10-08 04:29 Sun River 閱讀(259) 評論(0)  編輯  收藏 所屬分類: Servlet & Jsp
          主站蜘蛛池模板: 榕江县| 营山县| 南充市| 龙里县| 安宁市| 汉川市| 双鸭山市| 宁波市| 陈巴尔虎旗| 吉首市| 凭祥市| 抚松县| 西安市| 木兰县| 安顺市| 石嘴山市| 张家界市| 饶阳县| 佛教| 徐水县| 定陶县| 建水县| 乌兰察布市| 丰都县| 定日县| 苏尼特右旗| 土默特右旗| 于田县| 潢川县| 社会| 尤溪县| 达拉特旗| 清涧县| 拉萨市| 招远市| 博客| 濮阳市| 常德市| 丹东市| 怀化市| 锡林浩特市|