4

My use case is as follows: I have 10 threads simultaneously writing to one data structure. The order of the elements in the data structure does not matter. All the elements are unique. I will only be doing a read from this data structure only once at the very end.

What would be the fastest native Java data structure to suit this purpose? From my reading, it seems Collections.synchronizedList might be the go to option?

10
  • 2
    filling means you just need fast write or fast read as well? Commented Jan 26, 2016 at 15:50
  • Fast write only. I will only be doing one read at the very end. Commented Jan 26, 2016 at 15:53
  • 2
    Collections.synchronizedList will work, but you still need to choose the backing list (e.g. ArrayList, LinkedList, etc.). Since elements are unique you could also try sets such asConcurrentSkipListSet which will enforce uniqueness (perhaps that's useful to you?). Performance all depends on your read/write patterns. The best way to know will probably be to test it yourself. Commented Jan 26, 2016 at 15:55
  • 4
    Another possibility: avoid the synchronization entirely, write to 10 separate collections (one per thread), and only merge them at the end for the single read. Commented Jan 26, 2016 at 15:56
  • Is that you are just initialising your datastructure in the begining only or on an adhoc basis? Commented Jan 26, 2016 at 16:01

2 Answers 2

4

I have 10 threads simultaneously writing to one data structure.

I think it would be best to use a separate data structure per thread. That way no synchronisation is needed between the threads, and it would be much more CPU cache friendly too.

At the end they could be joined.

As for the underlying structure: if the elements are fixed size, an array/verctor would be best. Joining them would only take a copy of the block of memory they occupy, depending on the implementation - but lists would always be slower.

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

Comments

3

There is no need for you to synchronize on a list as each of the thread can work on their local copy and at the end can join results from all the threads into one final list.

If am going to use JDK7 and above then I would use fork and join for the same where i would create simple List in each forked task and finally join it in the main list at the end in the join phase.

If am on JDK6 then i could use a CountDownLatch with count as 10. Each and every thread after writing to their individual list (passed to the thread from main controller thread) counts down the latch and in the main controller, once all threads are done, i would combine all the result into one.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.