我正在编写一个必须以两种方式修改列表的程序。尽管此实现完美运行,但它无法让第二个线程获取锁:
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;
}
也许有人知道这是为什么?