引自:Spring Controller中全局变量引发的线程安全问题 - JavaShuo
在Spring中,注解@Controller去修饰一个类时,默认这个类是单例。可是WEB容器为了性能,必然是多线程的去处理HTTP请求,多线程的HTTP请求通过WEB容器转化为servlet请求,接着由Spring的DispatcherServlet分发到对应的Controller处理类。因此在单例的Controller类中,若是存在全局变量,必然会存在线程安全问题。安全
下面将示范多种经常使用写法,有些不会引发线程安全,有些会引发线程安全。多线程
示例1:app
这种写法不会引发线程安全,由于 HttpServletRequest 与 HttpServletResponse 申明为形参,形参是局部变量,而局部变量存储在栈中,每一个线程又有独立的栈空间,故不会引发线程安全问题。性能
@Controller public class TestController { @RequestMapping(value = "/test1") public void thread1(HttpServletRequest request, HttpServletResponse response) throws Exception{ System.out.println("客户端:" + request.getHeader("Thread")); } }
接下来启动10个线程去访问这个接口,在http请求的头部设置线程的名字,模拟10个不一样http客户端请求。this
public class HttpThread { public static void main(String[] args) { for (int i = 0; i < 10; i++) { new Thread(new Runnable(){ public void run(){ CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://localhost:8080/test1"); System.out.println(Thread.currentThread().getName()); httpGet.setHeader("Thread", Thread.currentThread().getName()); try { httpclient.execute(httpGet); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } } }
输出以下:
能够看到10个客户端都已经正常访问,没有线程安全问题。spa
示例2:
下面这种写法会引发线程安全问题。有时候一个Controller类中会有多个方法,为了代码的简洁,不想在多个方法中都要写HttpServletRequest request, HttpServletResponse response。把HttpServletRequest,HttpServletResponse 申明为全局变量 ,用@ModelAttribute注解修饰一个方法,并在这个方法中初始化request和response。就可在多处引用。可是这种写法会引发线程安全问题。线程
@Controller public class TestController { public HttpServletRequest request; public HttpServletResponse response; /** * ModelAttribute 注解表明只要调用TestController类的方法,就必定会先执行该方法, * 此处在方法内初始化了Servlet对象。 * @param request * @param response */ @ModelAttribute public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){ this.request = request; this.response = response; } @RequestMapping(value = "/test1") public void thread1() throws Exception{ System.out.println("客户端:" + request.getHeader("Thread")); } }
起30个线程去访问接口:code
public class HttpThread { public static void main(String[] args) { for (int i = 0; i < 30; i++) { new Thread(new Runnable(){ public void run(){ CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://localhost:8080/test1"); System.out.println(Thread.currentThread().getName()); httpGet.setHeader("Thread", Thread.currentThread().getName()); try { httpclient.execute(httpGet); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } } }
结果以下:
能够看到客户端13出现了屡次,而客户端4没有出现。此时若是这个接口里面有response。对象
response.getWriter().write(str);
那么,客户端4将接受不到返回,而且客户端13接收到的结果也有多是错误的。接口
解释一下为何会出现线程不安全的问题:
示例3:
使用@Autowired注解能够解决示例2的问题。代码以下:
@Controller public class TestController { @Autowired public HttpServletRequest request; @Autowired public HttpServletResponse response; @RequestMapping(value = "/test1") public void thread1() throws Exception{ System.out.println("客户端:" + request.getHeader("Thread")); } }
通过屡次实验,示例3的写法不会产生Servlet线程安全问题。你们也能够试一试。
后记:
具体@Autowired注入为何能够解决线程安全问题,这个估计要看源码才能知道。若是有高人知晓,请多多指教。