我正在使用ThreadSafeList,并且将数据从流程流传输到Web服务器,然后将数据流传输回客户端时,我从中得到了很大的收获。在内存中,我使用Spring Caching(后台ehcache)将数据保存在JVM中,一切都很好。当我开始达到我的堆限制并且Spring Caching开始在使用它的同时将ThreadSafeList序列化到磁盘时,麻烦就开始了,导致ConcurrentModificationExceptions。我可以覆盖序列化接口(interface)的私有(private)writeObject和readObject方法来解决此问题吗?我不确定如何执行此操作,或者不确定是否应该放弃ThreadSafeList。

回到我启动该程序时,我使用的是BlockingDeque,但这还不够,因为在放置和使用该结构时,我不记得要缓存的数据...我不能使用ConcurrentMap,因为我需要排序在我的列表中...我应该选择ConcurrentNavigableMap吗?我觉得自己想像一个ThreadSafeList和自定义的私有(private)序列化功能可能会浪费吗?

Java Code Geeks ThreadSafeList

最佳答案

Collections.synchronizedList()将使任何List成为线程安全的,并支持序列化(如果基础列表是Serializable)。如果需要遍历列表,请记住要在列表上进行同步。

例如

@Cacheable
public List<String> getData(String clientName) {
    List<String> data = Collections.synchronizedList(new ArrayList<String>());
    // load data for the client from external process and add it to the list...
    return data;
}

10-05 21:21