Reputation: 3623
Recently I started reading 'Java 7 Concurrency Cookbook' and in a section Creating and running a daemon thread found the code where main thread creates and one instance of ArrayDeque
and shares its reference with three producers and one consumer. The producers call deque.addFirst(event)
and the consumer calls deque.getLast()
.
But JavaDoc of ArrayDeque
clearly states that:
Array deques are not thread-safe; in the absence of external synchronization, they do not support concurrent access by multiple threads.
So I wonder whether it is a mistake or I just don't understand something?
Upvotes: 4
Views: 4145
Reputation: 19960
Array deques are not thread safe, meaning you have to provide external synchronization.
However why it works is, like holger said
You are using addFirst(e) is an insert model method which does causes change in underlying datastructure
You are using getLast() which is an examine model method which does not causes change in underlying datastructure.
That is why it is working, if you had used removeLast() instead of getLast(), you should have got ConcurrentModification Exception for sure.
Hope this clears up everything , Cheers
Upvotes: 2
Reputation: 7
It is clearly mentioned that if you are not going to provide any external synchronization, then ArrayDeque will not give you synchronization features just like Vector(provides internal features for thread safety-concurrency)
Upvotes: -2