Class SynchronizedCollection<E>
java.lang.Object
org.apache.commons.collections4.collection.SynchronizedCollection<E>
- Type Parameters:
E
- the type of the elements in the collection
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
- Direct Known Subclasses:
SynchronizedBag
,SynchronizedMultiSet
,SynchronizedQueue
Decorates another
Collection
to synchronize its behavior
for a multithreaded environment.
Iterators must be manually synchronized:
synchronized (coll) { Iterator it = coll.iterator(); // do stuff with iterator }
This class is Serializable from Commons Collections 3.1.
- Since:
- 3.0
- See Also:
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
SynchronizedCollection
(Collection<E> collection) Constructor that wraps (not copies).protected
SynchronizedCollection
(Collection<E> collection, Object lock) Constructor that wraps (not copies). -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends E> coll) void
clear()
boolean
boolean
containsAll
(Collection<?> coll) protected Collection
<E> Gets the collection being decorated.boolean
int
hashCode()
boolean
isEmpty()
iterator()
Iterators must be manually synchronized.boolean
boolean
removeAll
(Collection<?> coll) boolean
boolean
retainAll
(Collection<?> coll) int
size()
static <T> SynchronizedCollection
<T> synchronizedCollection
(Collection<T> coll) Factory method to create a synchronized collection.Object[]
toArray()
<T> T[]
toArray
(T[] object) toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, spliterator, stream
-
Field Details
-
lock
The object to lock on, needed for List/SortedSet views
-
-
Constructor Details
-
SynchronizedCollection
Constructor that wraps (not copies).- Parameters:
collection
- the collection to decorate, must not be null- Throws:
NullPointerException
- if the collection is null
-
SynchronizedCollection
Constructor that wraps (not copies).- Parameters:
collection
- the collection to decorate, must not be nulllock
- the lock object to use, must not be null- Throws:
NullPointerException
- if the collection or lock is null
-
-
Method Details
-
synchronizedCollection
Factory method to create a synchronized collection.- Type Parameters:
T
- the type of the elements in the collection- Parameters:
coll
- the collection to decorate, must not be null- Returns:
- a new synchronized collection
- Throws:
NullPointerException
- if collection is null- Since:
- 4.0
-
add
- Specified by:
add
in interfaceCollection<E>
-
addAll
- Specified by:
addAll
in interfaceCollection<E>
-
clear
- Specified by:
clear
in interfaceCollection<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
-
decorated
Gets the collection being decorated.- Returns:
- the decorated collection
-
equals
- Specified by:
equals
in interfaceCollection<E>
- Overrides:
equals
in classObject
-
hashCode
- Specified by:
hashCode
in interfaceCollection<E>
- Overrides:
hashCode
in classObject
-
isEmpty
- Specified by:
isEmpty
in interfaceCollection<E>
-
iterator
Iterators must be manually synchronized.synchronized (coll) { Iterator it = coll.iterator(); // do stuff with iterator }
-
remove
- Specified by:
remove
in interfaceCollection<E>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<E>
-
removeIf
- Specified by:
removeIf
in interfaceCollection<E>
- Since:
- 4.4
-
retainAll
- Specified by:
retainAll
in interfaceCollection<E>
-
size
- Specified by:
size
in interfaceCollection<E>
-
toArray
- Specified by:
toArray
in interfaceCollection<E>
-
toArray
- Specified by:
toArray
in interfaceCollection<E>
-
toString
-