Thread-safe sorted linked list
问题 I'm trying to write a thread-safe sorted single linked list . I wrote two versions: coarse grained synchronization and fine grained synchronization. Here are the two implementations: Fine grained: public void add(T t) { Node curr = head; curr.lock.lock(); while (curr.next != null) { // Invariant: curr is locked // Invariant: curr.data < t curr.next.lock.lock(); if (t.compareTo(curr.next.data) <= 0) { break; } Node tmp = curr.next; curr.lock.unlock(); curr = tmp; } // curr is acquired curr