【问题标题】:How to reduce the verbose redundancy of java thread local如何减少java thread local的冗长冗余
【发布时间】:2019-09-16 19:15:55
【问题描述】:

我有一个类,我想声明一个私有成员,它是一个线程本地字典。原来是这样的……

private static ThreadLocal<HashMap<Integer, Measurement>> measurements = 
        new ThreadLocal<HashMap<Integer, Measurement>>() 
{
    @Override protected HashMap<Integer, Measurement> initialValue()
    {
        return new HashMap<Integer, Measurement>();
    }

};

如您所见,我必须输入HashMap&lt;Integer, Measurement&gt; 的次数非常荒谬。有什么办法让这个更简洁吗?

【问题讨论】:

    标签: java thread-local


    【解决方案1】:

    这个怎么样?

    private static ThreadLocal<HashMap<Integer, Measurement>> measurements = ThreadLocal.withInitial(HashMap::new);
    

    【讨论】:

      猜你喜欢
      • 2014-08-28
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      相关资源
      最近更新 更多