threadlocal变量 [英] threadlocal variables in a servlet

查看:156
本文介绍了threadlocal变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于拥有变量的servlet的所有请求,threadlocals变量是否是全局变量?

Are the threadlocals variables global to all the requests made to the servlet that owns the variables?

我使用树脂作为服务器。

I am using resin for the server.

感谢awnser。

我想我可以让自己更清楚了。

I think I can make my self more clear.

具体情况:

我想:


  • 在请求开始执行时初始化静态变量。

  • 能够在线程安全中查询从servlet调用的方法的进一步执行中的变量值直到请求结束执行

推荐答案

$ b有点长一点:这就是Spring的魔法。请参见 RequestContextHolder (通过DocJar)。

Short answer: Yes.
A bit longer one: This is how Spring does its magic. See RequestContextHolder (via DocJar).

需要注意 - 你必须知道何时使ThreadLocal无效,如何推迟到其他线程,以及如何(非) -threadlocal context。

Caution is needed though - you have to know when to invalidate the ThreadLocal, how to defer to other threads and how (not) to get tangled with a non-threadlocal context.

或者你可以使用Spring ...

Or you could just use Spring...

这篇关于threadlocal变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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