threadlocals变量是否对拥有变量的servlet的所有请求都是全局的?
我在服务器上使用树脂.
感谢awnser.
我想我可以让自己更清楚.
具体案例:
我想要:
我最近看到一段代码使用了一个ThreadLocal对象并保留ConcurrentHashMap在其中.
这有什么逻辑/好处,还是多余的?
鉴于以下来自MSDN:
可以在任何线程上创建正则表达式对象并在线程之间共享.
我发现,对于性能,最好不要Regex在使用ThreadLocal类时在线程之间共享实例.
请问有人可以解释为什么线程本地实例的运行速度大约快5倍?
以下是结果(在8核机器上):
Using Regex singleton' returns 3000000 and takes 00:00:01.1005695
Using thread local Regex' returns 3000000 and takes 00:00:00.2243880
Run Code Online (Sandbox Code Playgroud)
源代码:
using System;
using System.Linq;
using System.Threading;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static readonly string str = new string('a', 400);
static readonly Regex re = new Regex("(a{200})(a{200})", RegexOptions.Compiled);
static void Test(Func<Regex> regexGettingMethod, string methodDesciption)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var sum = Enumerable.Repeat(str, 1000000).AsParallel().Select(s => regexGettingMethod().Match(s).Groups.Count).Sum(); …Run Code Online (Sandbox Code Playgroud) 我已经阅读了有关 ThreadLocal 及其使用的以下 SO 线程的有趣讨论。
这个问题更倾向于设计时选择。我的场景是这样的
如果我在 Web 应用程序中有一个值对象,几乎所有步骤都可能需要在同一个线程中使用它。我可以想到两个界面设计选项,如下所示
方法#1使用方法参数传递。
到目前为止,我一直专注于提出一个接口,该接口可以具有带有值对象接口参数的方法。
例如:
public interface SomeDataProcessorInterface {
public void processSomething(SomeValueObjectInterface vo);
}
public interface SomeValueObjectInterface extends Serializable {}
Run Code Online (Sandbox Code Playgroud)
方法 #2使用 ThreadLocal
在这种方法中,我可以有一个没有方法参数的接口,只需创建一个静态类来使用线程本地访问我的值对象。
例如:
public interface SomeDataProcessorInterface {
public void processSomething();
}
public interface SomeValueObjectInterface extends Serializable {}
public Class StaticClass {
private static ThreadLocal<SomeValueObjectInterface> threadLocalVO = new ThreadLocal<SomeValueObjectInterface>();
public static ThreadLocal getThreadLocal() {
return threadLocal;
}
Run Code Online (Sandbox Code Playgroud)
哪种方法更好?为什么?
这些实现中的哪一个实现内存泄漏的可能性较小?
这些实现中的哪一个对 …
请考虑以下代码:
#include <stdio.h>
__thread bool foo = true;
int
main() {
printf("foo = %d\n", foo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译并运行:
$ g++ tls.cpp -o tls -o tls
$ ./tls
Run Code Online (Sandbox Code Playgroud)
在某些系统上 - 例如Amazon Linux 2013.09.0,ami-5b792c32,内核3.4.62-53.42.amzn1.i686,g ++ 4.6.3 20120306(Red Hat 4.6.3-2) - 这会导致分段错误一旦foo被访问.
另一方面,foo在代码中显式初始化不会导致分段错误:
#include <stdio.h>
__thread bool foo = true;
int
main() {
foo = true; /* Added!! */
printf("foo = %d\n", foo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么第一个代码示例在某些系统上崩溃,而后者却没有?是否__thread变量的静态初始化不起作用?可能会破坏操作系统?
我在使用thread_local时遇到了一些奇怪的行为,并且不确定我是做错了什么还是GCC错误.我有以下最小的repro场景:
#include <iostream>
using namespace std;
struct bar {
struct foo {
foo () {
cerr << "foo" << endl;
}
int i = 42;
};
static thread_local foo FOO;
};
static thread_local bar::foo FREE_FOO;
thread_local bar::foo bar::FOO;
int main() {
bar b;
cerr << "main" << endl;
// cerr << FREE_FOO.i << endl;
cerr << b.FOO.i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用上面的注释行,输出如下所示:
main
0
Run Code Online (Sandbox Code Playgroud)
随着它取消注释,它变成了这样:
main
foo
foo
42
42
Run Code Online (Sandbox Code Playgroud)
我只是在这里错过了一些愚蠢的东西吗?
$ gcc -v
Using built-in specs. …Run Code Online (Sandbox Code Playgroud) ThreadLocal 在 Java 中说:
Java 中的 ThreadLocal 类使您能够创建只能由同一线程读取和写入的变量。因此,即使两个线程正在执行相同的代码,并且该代码引用了一个 ThreadLocal 变量,那么两个线程也无法看到彼此的 ThreadLocal 变量。
我的问题是:当我们需要获取特定于线程的变量时,我们不能在方法中将该变量声明为局部变量吗?因为每个线程都有自己的堆栈,因此它有自己的变量副本。我在这里错过了什么吗?
让我们以SimpleDateFormat为例,因为它不是线程安全的.
我可以允许每个线程使用threadLocal拥有自己的SimpleDateFormat副本,如下所示:
private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue()
{
return new SimpleDateFormat("yyyyMMdd HHmm");
}
};
Run Code Online (Sandbox Code Playgroud)
但volatile关键字保证线程将拥有该变量的最新副本.所以我不能这样做:
volatile SimpleDateFormat myformatter;
Run Code Online (Sandbox Code Playgroud)
并实现相同的线程安全?
我的代码在 Java 8 中运行良好,但是当我将其迁移到 Java 17 时,它就不起作用了。它涉及 ThreadLocal 和 CompletableFuture.runAsync。
以下是课程:
public class UriParameterHandler {
}
public class DateRangeEntity {
public String getCurrentDate(){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
return dtf.format(now);
}
}
public class SessionHandler {
private static ThreadLocal<SessionHandler> instance = new InheritableThreadLocal<>();
private UriParameterHandler uriParameterHandler;
private DateRangeEntity dateRangeEntity;
private SessionHandler() {
instance.set(this);
}
public static void initialize() {
SessionHandler handler = new SessionHandler();
handler.uriParameterHandler = new UriParameterHandler();
}
public static UriParameterHandler getUriParameterHandler() {
return instance.get().uriParameterHandler; …Run Code Online (Sandbox Code Playgroud) 我想知道我是否可以在Akka Futures中使用tinkerpop,到目前为止,当我对图表进行更改时,它们不会被持久化.我知道tinkerpop是一个线程本地库,这意味着我需要在将来再次设置我的线程ODatabaseRecordThreadLocal.INSTANCE.set(thread)
我尝试了以下方法但没有成功:
def test[T](graphChanges: => T): T = {
val thread = ODatabaseRecordThreadLocal.INSTANCE.get
try graphChanges finally {
ODatabaseRecordThreadLocal.INSTANCE.set(thread)
GraphPool.get("partitioned").commit
}
}
// collect tinkerpop frames
test {
future {
// add changes to my tinkerpop frames
}
}
Run Code Online (Sandbox Code Playgroud)
我想在每个play.mvc.Http.Context上有Tinkerpop线程
以下是我想要实现的示例项目:https://github.com/D-Roch/tinkerpop-play
thread-local ×10
java ×6
c++ ×2
concurrency ×2
.net ×1
akka ×1
c++11 ×1
construction ×1
gcc4.8 ×1
interface ×1
java-17 ×1
linux ×1
performance ×1
regex ×1
scala ×1
servlets ×1
stack ×1
tinkerpop ×1
volatile ×1