is the servlet thread-safe? This question, did not see an exact answer on the net, so let us analyze:
First, what is thread safety?
Reference concept: If your code is in a process where multiple threads are running concurrently, these threads may run this code at the same time. If the result of each run is the same as the single-threaded run, and the value of the other variable is the same as expected, it is thread-safe.
We all know that servlets are multithreaded, while a servlet implementation class has only one instance object, which is singleton, so multiple threads are likely to access the same Servlet instance object.
Each thread will open a separate reference for the data instance object, will the servlet be thread-safe?
To determine whether it is thread-safe, we need to know what is causing the thread safety problem.
Search for answers: Thread safety issues are caused by global variables and static variables.
This is a description found on the Internet: A bug that was investigated many years ago, when the legacy code in our system wrote a lot of global variables, once released, customer feedback, when there are many people doing something at the same time, our data out of the problem, when we investigate the result is: multi-person synchronous operation, Some global variables have incorrect values, and after that we dedicate a lot of time to changing all global variables to local variables, and the project requires that global variables not be allowed in the future. It turned out that I had encountered a thread unsafe situation, ah, but the processing mode or not the global, or join the synchronization, if you join the synchronization also have to consider the efficiency of the program will not have an impact.
It can be seen that whether the servlet thread safety is determined by its implementation, if its internal properties or methods will be changed by multiple threads, it is thread insecure, and vice versa, is thread-safe.
Find an example on the Internet, as follows:
public class Testservlet extends HttpServlet {
private int count = 0;
@Override
protected void Service (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.getwriter (). println ("<HTML><BODY>");
Response.getwriter (). println (This + "==>");
Response.getwriter (). println (Thread.CurrentThread () + ": <br>");
for (int i=0;i<5;i++) {
Response.getwriter (). println ("Count =" + Count + "<BR>");
try {
Thread.Sleep (1000);
count++;
} catch (Exception e) {
E.printstacktrace ();
}
}
Response.getwriter (). println ("</BODY></HTML>");
}
}
When you open more than one browser at the same time, when you enter Http://localhost:8080/ServletTest/TestServlet, the results are different, which means that for attribute count, it is thread insecure,
To solve this problem, refactor the code as follows:
public class Testservlet extends HttpServlet {
private int count = 0;
Private String synchronizestr = "";
@Override
protected void Service (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.getwriter (). println ("<HTML><BODY>");
Response.getwriter (). println (This + "==>");
Response.getwriter (). println (Thread.CurrentThread () + ": <br>");
Synchronized (SYNCHRONIZESTR) {
for (int i=0;i<5;i++) {
Response.getwriter (). println ("Count =" + Count + "<BR>");
try {
Thread.Sleep (1000);
count++;
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Response.getwriter (). println ("</BODY></HTML>");
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Whether the servlet is thread safe