I have a linked list samples
:
protected LinkedList samples = new LinkedList();
I\'m append
if you have a JDK, you can look at the source code of "Collections.synchronizedList()". It is simple, so you can create a copy of this method specialized to get both LinkedList and synchronization functionnalities.
public class SynchronizedLinkedList<T> implements List<T> {
private LinkedList<T> list;
private Object lock;
public void add(T object) {
synchronized(lock) {
list.add(object);
}
}
// etc.
}
No LinkedList is not thread safe. Use LinkedBlockingDeque instead
That is correct - LinkedList is not synchronized and thus not thread safe. If you do not want to use the newer synchronized analogies of LinkedList, namely, ConcurrentLinkedQueue or LinkedBlockingQueue, you can initialize LinkedList like this:
LinkedList<RawDataset> samples = (LinkedList)Collections.synchronizedList(new LinkedList<RawDataset>());
LinkedList is not thread safe. You'd have to do the locking yourself.
Try ConcurrentLinkedQueue or LinkedBlockingDeque instead if it fits your needs, they are thread safe but slightly different behavior than LinkedList.