如果对某个业务类Service 采用了单例模式(饿汉式),那么其他类通过getInstance()获得的实例去访问该service 的方法,是否存在线程安全问题?代码如下
public class UrlService {
private static UrlService instance = new UrlService();
private UrlService() {
}
public static UrlService getInstance() {
return instance;
}
public void addUrl(HttpServletRequest request) {
Stack<String> urlStack = (Stack<String>) request.getSession().getAttribute("stack");
urlStack.push(url);
request.getSession().setAttribute("stack", urlStack);
}
public String getUrl(HttpServletRequest request) {
Stack<String> urlStack = (Stack<String>) request.getSession().getAttribute("url");
String url = urlStack.pop();
request.getSession().setAttribute("stack", urlStack);
return url;
}
}
多个线程访问addUrl和getUrl时是否是线程安全的?
更多 0 |