我的 ThreadLocal 始终包含 returns null

My ThreadLocal contains and returns null always

我想知道 threadlocal.set() 在我将其设置为 32 个元素的集合时如何没有存储任何数据。 ThreadLocal.get() returns 始终为空;并且相应的 FutureTask 对象有一个结果 属性 = NullPointerException。知道为什么 ThreadLocal 无法存储集合项吗?

public class MyCallable<T> implements Callable<Collection<T>> {

    public MyCallable( Collection<T> items ){
        tLocal = new ThreadLocal<Collection<T>>();
        tLocal.set( items );                    //SETS NULL ALTHOUGH the PARAMETER CONTAINS 32 ITEMS
    }

    @Override
    @SuppressWarnings("unchecked")
    public Collection<T> call() throws Exception {
        synchronized( lock ){
            ArrayList<T> _items = new ArrayList<T>();
            ArrayList<T> _e = ( ArrayList<T> ) tLocal.get();   //RETURNS NULL
            for( T item : _e ){
                _items = getPValue( item ));
            }
            return _items ;
        }
    }

    private ThreadLocal<Collection<T>> tLocal;

    private final Object lock = new Object();
}

用法片段:

List<Future<Collection<T>>> futures = new ArrayList<Future<Collection<T>>>();
ExecutorService pool = Executors.newFixedThreadPool( 8 );

        for( int x = 0; x < numBatches; ++x ){
            List<T> items = retrieveNext32Items( x );
            futures.add( pool.submit( new MyCallable<T>( items ));
        }

        pool.shutdown();

        for( Future<Collection<T>> future : futures ) {
            _items.addAll( future.get() );                  //future.outcome = NullPointerException 
        }

        return _items
}

您在主线程中创建类型为 MyCallable 的对象,然后将它们提交到线程池。因此 MyCallable 的构造函数在一个线程中被调用,方法 call 在另一个线程中被调用。 Thread local 为每个线程保留一个单独的数据,所以难怪你会得到空值。

我不明白你为什么使用本地线程。 items 应该是 MyCallable 中的一个简单字段。如果您修改了集合,也许将其复制到新集合中会更好。

存储在本地线程中的值是为特定线程存储的。因此,如果您将值存储在特定线程 t1 的本地线程中,并尝试使用同一本地线程从另一个线程 t2 获取该值……您将无法获取该值,但会获取 null。检查在线程本地设置值的线程是否与从线程本地检索值的线程相同