java - ThreadLocal 和列表不起作用

标签 java thread-safety thread-local

我的基于 ThreadLocal 的类遇到问题。任何帮助,将不胜感激。这是一个带有简单列表的基类:

public class ThreadLocalTest {

protected static final ThreadLocal<List<String>> thList = new ThreadLocal<List<String>>() {
    protected List<String> initialValue() {
        return new ArrayList<String>();
    }
};

public static void put(String k) {
    thList.get().add(k);
}

public static List<String> getList() {
    return thList.get();
}

}

我正在这样测试:

Thread th1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("------------------thread1---------------------------");
                ThreadLocalTest.put("a");
                ThreadLocalTest.put("b");
                List<String> l = ThreadLocalTest.getList();
                System.out.println(l.size());
                System.out.println("----------------------------------------------------");
            }
        });
        Thread th2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("------------------thread2---------------------------");
                ThreadLocalTest.put("c");
                List<String> l = ThreadLocalTest.getList();
                System.out.println(l.size());
                System.out.println("----------------------------------------------------");
            }
        });
        th1.run();
        th2.run();
        th1.run();
        th2.run();
        th1.run();
        th2.run();
        th1.run();
        th2.run();

所以我得到的是:

------------------thread1---------------------------
2
----------------------------------------------------
------------------thread2---------------------------
3
----------------------------------------------------
------------------thread1---------------------------
5
----------------------------------------------------
------------------thread2---------------------------
6
----------------------------------------------------
------------------thread1---------------------------
8
----------------------------------------------------
------------------thread2---------------------------
9
----------------------------------------------------
------------------thread1---------------------------
11
----------------------------------------------------
------------------thread2---------------------------
12
----------------------------------------------------

您会发现这些线程实际上共享相同的列表,但我不明白为什么。

有什么建议吗?

最佳答案

您调用run()方法而不是start()run() 在调用它的同一线程中运行,而 start() 在新的单独线程中调用 run()。实际上,所有“线程”都在同一个线程中执行。

关于java - ThreadLocal 和列表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18630950/

相关文章:

java - 如何在返回 HTTP 代码后通知客户端异常

multithreading - 使用 Go 1.2 的线程 CGO

java - ThreadLocal - 如果我们在构造函数中设置值,是否需要它?

C++ : Handle thread-local object destruction

java - "ParentClass objectName1= new SubClass() "和 "SubClass objectName2 = new SubClass()"有什么区别?

c# - 将两条短裤放入字节数组

python - uuid.uuid4() 在具有多处理功能的 Windows 上使用是否安全?

java - 最小化多线程环境中的 SecureRandom 性能问题?

java - 从用于使用 Google Guava 编制索引的文件读取引起的 NullPointerException

windows - 如何在 Perl 中线程安全地设置 STDOUT 编码?