Multi-thread synchronization in Servlet and JSP

Source: Internet
Author: User

Compared with ASP and PHP, Servlet/jsp (preferred for SUN enterprise-level applications) has a high execution efficiency due to its multi-threaded operation. Servlet/jsp (preferred for SUN Enterprise Applications) is executed in multi-threaded mode by default. Therefore, you must carefully consider the synchronization of multiple threads when writing code. However, many developers did not notice the problem of multi-thread synchronization when writing Servlet/jsp (the preferred choice for SUN Enterprise Applications) programs, which often caused no problems in programming when a small number of users access it, when concurrent users reach a certain value, some unknown problems often occur, and debugging is difficult for such random problems.

  I. variable types in Servlet/jsp (preferred for SUN Enterprise applications)

Be careful when writing Servlet/jsp programs (preferred for SUN Enterprise applications. Because instance variables are non-thread-safe. In Servlet/jsp (the preferred choice for SUN Enterprise applications), variables can be classified into the following categories:

  1. class variables

Request, response, session, config, application, and pages and pageContext built in jsp (preferred for SUN Enterprise applications. All applications are thread-safe.

  2. instance variables

Instance variables are owned by instances and allocated in the heap. In Servlet/jsp containers (preferred for SUN Enterprise applications), only one Servlet/jsp (preferred for SUN Enterprise Applications) instance is instantiated, start Multiple Threads of this instance to process requests. Instance variables are shared by all threads of the instance. Therefore, instance variables are not thread-safe.

  3. Local Variables

Local variables are allocated in the stack. Because each thread has its own execution stack, local variables are thread-safe.

  Ii. multithreading synchronization in Servlet/jsp (preferred for SUN Enterprise applications)

In jsp (the preferred choice for SUN Enterprise applications), be cautious when using instance variables. First, see the following code:

     // Instanceconcurrenttest. jsp (preferred for SUN Enterprise Applications) <% @ page contentType = "text/html; charset = GBK" %> <%! // Define the instance variable String username; String password; java. io. printWriter output; %> <% // obtain the parameter username = request from the request. getParameter ("username"); password = request. getParameter ("password"); output = response. getWriter (); showUserInfo (); %> <%! Public void showUserInfo () {// to highlight the concurrency problem, run a time-consuming operation int I = 0; double sum = 0.0; while (I ++ <200000000) {sum + = I;} output. println (Thread. currentThread (). getName () + "<br>"); output. println ("username:" + username + "<br>"); output. println ("password:" + password + "<br>") ;}%>


On this page, we first define two instance variables: username and password. Then obtain the two parameters from the request and call the showUserInfo () method to display the information of the requesting user in the client's browser. There is no problem during a user access. However, when multiple users access the service concurrently, the information of other users is displayed on the browsers of other users. This is a serious problem. To highlight concurrency issues and facilitate testing and observation, we performed a simulated time-consuming operation when returning the user information. For example, the following two users can simultaneously access the site (two ie browsers can be started, or access the service on both machines at the same time ):

A: http: // localhost: 8080/instanceconcurrenttest. jsp (preferred for SUN Enterprise Applications )? Username = a & password = 123

B: http: // localhost: 8080/instanceconcurrenttest. jsp (preferred for SUN Enterprise Applications )? Username = B & password = 456

If a clicks the link and B clicks the link again, a returns a blank screen, and B gets the output of both threads a and B. See the following screen:



Figure 1: a's screen



Figure 2: B's screen

The running result shows that the Web server starts two threads to process requests from a and B respectively, but a gets a blank screen in. This is because the output, username, and password in the above program are both instance variables and are shared by all threads. After a accesses this page, it sets output to a's output, username, and password to a's information respectively. Before a executes printUserInfo () to output username and password information, B visited the page again, set username and password to B, and point the output to B. When the thread of a is printed, it is printed to the screen of B, and the user name and password of a are also replaced by B. See:

9 7 3123 4 8:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.