4

I have many threads adding result-like objects to an array, and would like to improve the performance of this area by removing synchronization.

To do this, I would like for each thread to instead post their results to a ThreadLocal array - then once processing is complete, I can combine the arrays for the following phase. Unfortunately, for this purpose ThreadLocal has a glaring issue: I cannot combine the collections at the end, as no thread has access the collection of another.

I can work around this by additionally adding each ThreadLocal array to a list next to the ThreadLocal as they are created, so I have all the lists available later on (this will require synchronization but only needs to happen once for each thread), however in order to avoid a memory leak I will have to somehow get all the threads to return at the end to clean up their ThreadLocal cache... I would much rather the simple process of adding a result be transparent, and not require any follow up work beyond simply adding the result.

Is there a programming pattern or existing ThreadLocal-like object which can solve this issue?

4
  • 1
    You can use ConcurrentHashMap instead of ThreadLocal array and use your result object as key in map. Commented May 3, 2016 at 5:03
  • Just have each thread produce "result-like objects" which include where those objects should be inserted into the final array, e.g. include the array indices. Commented May 3, 2016 at 5:05
  • Is the number of results known in advance? What is the impetus that causes a thread to be assigned the task of producing a result? A set of data present at the beginning? random events arriving asynchronously? Commented May 3, 2016 at 5:10
  • @HankD Results are produced by a thread pool, so random results come from random threads. Some tasks can produce multiple results, so there will be an unknown number. Commented May 3, 2016 at 5:14

1 Answer 1

1

You're right, ThreadLocal objects are designed to be only accessible to the current thread. If you want to communicate across threads you cannot use ThreadLocal and should use a thread-safe data structure instead, such as ConcurrentHashMap or ConcurrentLinkedQueue.

For the use case you're describing it would be easy enough to share a ConcurrentLinkedQueue between your threads and have them all write to the queue as needed. Once they're all done (Thread.join() will wait for them to finish) you can read the queue into whatever other data structure you need.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.