In Java, I need an object which is an ArrayList
of 60 LinkedBlockingQueue
s. The ArrayList
will .add()
60 LinkedBlockingQueue
s in the beginning of the program, while still single threaded. Then one thread will .add()
to the LinkedBlockingQueue
s while another thread will .poll()
. Is there any need to explicitly synchronize this object?
Asked
Active
Viewed 205 times
0
2 Answers
4
Nope, if the list isn't modified after initialization then you don't need to synchronize it. I recommend you use an UnmodifiableList instead of an ArrayList - you'll get a runtime exception if anybody tries to modify the list after it's initialized, which is preferable to having a race condition.

Zim-Zam O'Pootertoot
- 17,888
- 4
- 41
- 69
0
Is there any need to explicitly synchronize this object?
If you fork the threads after you finish building the List
then you are fine. However, I'd use an array instead of a Collection if it is truly immutable. You can just do:
BlockingQueue[] blockingQueueArray =
blockingQueueList.toArray(new BlockingQueue[blockingQueueList.size()]);

Gray
- 115,027
- 24
- 293
- 354