How to make a list thread safe for serialization?

I use ThreadSafeList and I get a lot of mileage from it for streaming data from a process to a web server, and then streaming data output when it arrives at the client. In memory, I store data in the JVM using Spring Caching (ehcache under the hood), and all is well. The problem started when I started clicking on the edges of heaps and Spring. Caching has begun serializing my ThreadSafeList to disk while I use it, calling ConcurrentModificationExceptions. Can I overwrite the private writeObject and readObject methods for the Serialization interface to solve the problem? I am not sure how to do this, or if I should give up my ThreadSafeList.

When I started this program, I used BlockingDeque, but that was not enough, because when I added and accepted the structure, I could not remember the data for caching ... I can not use ConcurrentMap because I need to order from my list. .. should I go for ConcurrentNavigableMap? I feel like I'm skating on my own with ThreadSafeList, and may custom serialization functions be unnecessary?

Java Code Geeks ThreadSafeList

+6
source share
1 answer

Collections.synchronizedList() will make any thread thread safe and support serialization (if the underlying list is Serializable). Remember to sync in the list if you need to iterate over it.

eg.

 @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; } 
+1
source

Source: https://habr.com/ru/post/984237/


All Articles