I'm writing a program that has to modify a List in two ways. Although this implementation works perfectly, it fails to let the second thread acquire the lock:
Node head = new Node(new Object(), null);
public static ReentrantLock lock = new ReentrantLock();
...
boolean add(Object o){
lock.lock();
Node current = head;
Node previous = head.next;
if(head.next==null){
head.addNext(new Node(o,null));
return true;
}
while(!(current.next == null)){
current=current.next;
previous=previous.next;
}
current.addNext(new Node(o,null));
lock.unlock();
return true;
}
Maybe someone knows why is that?