线程安全的控制器和实用程序类? [英] Thread-safe Controller and Utility Classes?

查看:71
本文介绍了线程安全的控制器和实用程序类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用Spring MVC,并且在我的控制器中调用了几个Utility类.我在这些实用工具类中使用的集合是否需要同步?同样,每个用户在控制器中访问我的网页时是否会产生多个线程,这意味着我需要确保线程安全?

So I'm using Spring MVC and in my controller I call several Utility classes. Do the Collections I use in those utility classes need to be synchronized? Similarly, are multiple threads spawned for each user when they access my webpage in the controller meaning I need to ensure thread-safety?

推荐答案

每个请求将由servlet容器(从线程池)分配的任意线程处理,因此多个请求将意味着控制器的多个并发执行.用户和线程之间没有直接关联,只有请求和线程之间没有直接关联,但是如果您有多个用户,则通常会有并发请求,因此有多个线程.

Each request will be handled by some arbitrary thread allocated by the servlet container (from a thread pool), so multiple requests will mean multiple concurrent executions of the controller. There is no direct correlation between users and threads, just requests and threads, but if you have multiple users, then you typically have concurrent requests, and so multiple threads.

鉴于控制器应该是线程安全的,那么您将需要确保通过设计(例如,使它们或控制器请求或可能的会话范围,如果确保了它们的作用)来确保控制器线程安全性所使用的实用程序类和集合.不能同时为同一会话提供服务)或使用共享资源锁定.

Given that controllers should be thread-safe, you will then need to ensure your utility classes and collections used by the controller thread-safe, either by design (e.g. making them or the controller request or possibly session scope if you ensure the same session cannot be served concurrently) or by use of locking on shared resources.

这篇关于线程安全的控制器和实用程序类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆