简体   繁体   English

我的ThreadLocal包含并始终返回null

[英]My ThreadLocal contains and returns null always

I am wondering how the threadlocal.set() has no data stored when I set it to a collection of 32 elements. 我想知道当我将threadlocal.set()设置为32个元素的集合时,如何不存储任何数据。 ThreadLocal.get() returns null always; ThreadLocal.get()始终返回null; and the correspoding FutureTask object has an outcome property = NullPointerException. 并且相应的FutureTask对象具有一个结果属性= NullPointerException。 Any idea why ThreadLocal is unable to store collection items? 知道为什么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();
}

usage snippet: 使用摘要:

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
}

You create object of type MyCallable in the main thread then submit them to a thread pool. 您在主线程中创建MyCallable类型的对象,然后将其提交到线程池。 So constructors of MyCallable are called in one thread and methods call in another. 因此,MyCallable的构造函数在一个线程中call ,而方法在另一个线程中call Thread local keeps a separate data for each thread so no wonder you get nulls. 线程本地为每个线程保留一个单独的数据,所以难怪您会得到null。

I don't understand why you use thread local. 我不明白为什么要使用本地线程。 items should be a simple field in MyCallable. items应该是MyCallable中的一个简单字段。 If you modify the collection maybe it would be better to copy it to a new collection. 如果您修改集合,则最好将其复制到新集合中。

The value stored in a thread local is stored for the particular thread . 存储在本地线程中的值是为特定线程存储的。 So if you store a value in a thread local for a particular thread t1 and try to obtain the value from another thread t2 using the same thread local...you would not get the value but would get null. 因此,如果将值存储在特定线程t1的本地线程中,并尝试使用相同的线程本地从另一个线程t2获取该值,则不会得到该值,但会得到null。 Check whether the thread in which the value is set in the thread local is the same thread from which the value is being retrieved from the thread local 检查在线程本地中设置值的线程是否与从线程本地中检索值的线程相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM