multithreading - In Java, is there a deadlock if `insert()` and `size()` executed in concurrency? -


the codes looks (link):

/***  * excerpted "seven concurrency models in 7 weeks", ***/ import java.util.concurrent.locks.reentrantlock;  class concurrentsortedlist {    private class node {     int value;     node prev;     node next;     reentrantlock lock = new reentrantlock();      node() {}      node(int value, node prev, node next) {       this.value = value; this.prev = prev; this.next = next;     }   }    private final node head;   private final node tail;    public concurrentsortedlist() {     head = new node(); tail = new node();     head.next = tail; tail.prev = head;   }    public void insert(int value) {     node current = head;     current.lock.lock();      node next = current.next;     try {       while (true) {         next.lock.lock();          try {           if (next == tail || next.value < value) {              node node = new node(value, current, next);              next.prev = node;             current.next = node;             return;            }         } { current.lock.unlock(); }          current = next;         next = current.next;       }     } { next.lock.unlock(); }    }    public int size() {     node current = tail;     int count = 0;      while (current.prev != head) {       reentrantlock lock = current.lock;       lock.lock();       try {         ++count;         current = current.prev;       } { lock.unlock(); }     }      return count;   }  } 

it says uses hand-over-hand locking. insert() lock list head list tail, , size() lock list tail list head. size() , insert() can executed in concurrency.

but think size() , insert() cannot executed in concurrency. because if insert holding lock on anode , request lock on anode.next, while size holding lock on anode.next , request lock on anode, there deadlock.

does have ideas this? thanks!

i see.. size() release current lock before requests new lock.. there no deadlock..


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -