当锁定操作等待时,线程永远被阻塞

问题描述:

我正在为两阶段锁定编写一个java实现。所以,我使用了Reentrant锁(ReadWrite锁)。问题是,当一个线程执行lock.readLock.lock()或lock.writeLock()。lock()并且锁已经被锁定时,即使通过使用lock.readLock()来解锁锁,它也会永久卡住。 ()或lock.writeLock()。unlock()。所以,它看起来像解锁不唤醒服务员! 这里是导致该问题的代码:当锁定操作等待时,线程永远被阻塞

class LockTable 
{ 
// /******************************************************************************* 
//  * This class is used to represent an individual lock. 
//  * @param tid  the id of the transaction holding the lock 
//  * @param shared whether the lock is shared (true) or exclusive (false) 
//  */ 
// void Lock (int tid, boolean shared) 
// { 
//  Semaphore sem = new Semaphore (0); 
// } // Lock class 

    /** Associative map of locks held by transactions of the form (key = oid, value = lock) 
    */ 
    private HashMap<Integer,MyLock> locks; 

    public LockTable(){ 
     locks= new HashMap<Integer,MyLock>(); 

    } 

    /******************************************************************************* 
    * Acquire a shared/read lock on data object oid. 
    * @param tid the transaction id 
    * @param oid the data object id 
    */ 
    void rl (int tid, int oid) throws InterruptedException 
    { 

     MyLock lock=null; 
     boolean wait = false; 
     synchronized(this) { 
      try { 
       lock = locks.get(oid);    // find the lock 

       if((lock != null) && (lock.lock.isWriteLocked())){ 

        wait = true; 

        // System.out.println(locks.get(oid).shared); 
       } 

       if(lock == null){ 
       lock = new MyLock(tid, true); 
       lock.lock.readLock().lock(); 
       lock.readers.add(tid); 
       locks.put(oid, lock); 
       } 

      } catch(Exception e) { 
       System.out.println(e.getStackTrace());  // lock not found, so oid is not locked; 
      } // try 
     }//synch 


     if (wait){ 

      System.out.println("Transaction " + tid + " is waiting.."); 
      Main.g.addEdge(tid, lock.tid); 
        if(Main.g.hasCycle()) 
         restart(tid); 

      //to exclude the restarted thread 
      if(!Main.trans[tid].terminate){ 

       lock.lock.readLock().lock(); 
       Main.g.removeEdge(tid, lock.tid); 
       synchronized(this){ 
       lock.readers.add(tid); 
       }//synchronized 
      }//if isInturrupted 
      else 
       return; 
     } 
     else 
     synchronized(this) { 
       lock.lock.readLock().lock(); 
       lock.readers.add(tid); 
     } // synchronized 

    } // rl 

    /******************************************************************************* 
    * Acquire an exclusive/write lock on data object oid. 
    * @param tid the transaction id 
    * @param oid the data object id 
    */ 
    void wl (int tid, int oid) throws InterruptedException 
    { 
     //type to determine the last lock type in order 
     //to be able to remove the edges from waitfor graph 
     int type = 0; 
     MyLock lock = null; 
     boolean wait = false; 

     synchronized(this) { 
      try { 
       lock = locks.get(oid);    // find the lock 
       if(lock != null && (lock.lock.isWriteLocked() || lock.readers.size() > 0)) 
       { 
        wait = true; 
       } 
       if(lock == null){ 
        lock = new MyLock(tid); 
        lock.lock.writeLock().lock(); 
        locks.put(oid,lock); 
       } 
      } catch(Exception e) { 
       System.out.println(e.getStackTrace());  // lock not found, so oid is not locked; 
      } // try 
     } 
     if (wait){ 
       System.out.println("Transaction " + tid + " is waiting.."); 
       if(lock.lock.isWriteLocked()) 
        Main.g.addEdge(tid, lock.tid); 
       else{ 
        type = 1; 
        for(int reader : lock.readers) 
         Main.g.addEdge(tid, reader); 
        }//else 

          if(Main.g.hasCycle()) 
          { 
           restart(tid); 
          }//if 
      if(!Main.trans[tid].terminate){ 
       System.out.println("I'm waiting here in wl"); 
       lock.lock.writeLock().lock(); 
       System.out.println("Wakeup.."); 
       if(type == 0) 
        Main.g.removeEdge(tid, lock.tid); 
       else 
        for(int reader : lock.readers) 
         Main.g.removeEdge(tid, reader); 
       lock.tid = tid; 
      } 
      else 
       return; 

     }// if(wait) ==> for the lock to be released 
     else 
      lock.lock.writeLock().lock(); 

    } // wl 

    void restart(int tid){ 
    synchronized(this) { 
     MyLock lock; 
     List<Integer> toRemove = new ArrayList(); 
     for(int i : locks.keySet()){ 
      lock = locks.get(i); 

       //lock.sem.release(); 
       if(lock.lock.isWriteLockedByCurrentThread()){ 

        System.out.println("Transaction"+tid+" unlock object "+ i +" in order to restart"); 
        lock.lock.writeLock().unlock(); 
        System.out.println("number of write holders: " + lock.lock.writeLock().getHoldCount()); 
        System.out.println("number of read holders: " + lock.lock.getReadHoldCount()); 
        System.out.println("number of waiters: " + lock.lock.getQueueLength()); 
        toRemove.add(i); 

       } 
      if(!lock.lock.isWriteLocked()) 
       if(lock.readers.contains(tid)){ 
        // lock.numberOfReaders --; 

         System.out.println("Transaction"+tid+" unlock object "+ i +" in order to restart"); 
         lock.readers.remove(lock.readers.indexOf(tid)); 
         lock.lock.readLock().unlock(); 
         System.out.println("number of write holders: " + lock.lock.getWriteHoldCount()); 
         System.out.println("number of read holders: " + lock.lock.getReadHoldCount()); 
         System.out.println("number of waiters: " + lock.lock.getQueueLength()); 
         toRemove.add(i); 

        }//if 
     }//for 
     for(int i = 0; i < toRemove.size() ; i ++) 
      locks.remove(toRemove.get(i)); 
     Main.g.removeEdges(tid); 

     // Thread.currentThread().interrupt(); 
     Main.trans[tid].terminate = true; 

     System.out.println("Transaction" + tid + " restarted"); 

     }//sync 
    } 

    /******************************************************************************* 
    * Unlock/release the lock on data object oid. 
    * @param tid the transaction id 
    * @param oid the data object id 
    */ 
    void ul (int tid, int oid) 
    { 
     MyLock lock = null; 
     boolean error = false; 
     synchronized(this) { 
      try { 
       lock = locks.get(oid);     // find the lock 
       if(lock == null) 
        System.out.println("println: lock not found"); 

      } catch(Exception e) { 
       System.out.println("lock not found"); // lock not found 
      } // try 
     }//sync 
      if((lock != null) && (lock.lock.isWriteLockedByCurrentThread())){ 

         System.out.println("tid: " + tid + " unlock object: " + oid); 
         lock.lock.writeLock().unlock(); 
         System.out.println("done with unlock"); 
         System.out.println("number of write holders: " + lock.lock.writeLock().getHoldCount()); 
         System.out.println("number of read holders: " + lock.lock.getReadHoldCount()); 
         System.out.println("number of waiters: " + lock.lock.getQueueLength()); 
      }// if lock != null 
      else 
       if((lock != null) && (lock.readers.size()>0)){ 
        if(lock.readers.contains(tid)){ 
        lock.readers.remove(lock.readers.indexOf(tid)); 
        lock.lock.readLock().unlock(); 
        System.out.println("Transaction"+tid+" unlocked shared lock on object "+oid); 
        //System.out.println("number of write holders: " + lock.lock.readLock().); 
        System.out.println("number of read holders: " + lock.lock.getReadHoldCount()); 
        System.out.println("number of waiters: " + lock.lock.getQueueLength()); 

        }//if lock.readers 
       }//if 


     if (error) 
      System.out.println ("Error: ul: no lock for oid = " + oid + " found/owned"); 
    } // ul 
+0

你可以清理一下,只留下相关的代码行吗?这有点难以阅读...... – mre 2011-04-05 23:08:01

+0

你会得到哪个输出,以及你期望输出哪个输出?你的例子不能以这种方式执行,所以我们不能试用它。 – 2011-04-05 23:21:27

+0

此外,您可能想使用'jstack'工具查看卡住时所有线程的堆栈状态。它向您显示哪个线程保存哪个锁,哪个线程正在等待哪个锁。 – 2011-04-05 23:22:40

Method A: 
...snip... 
synchronized(this) { 
    ...snip... 
    lock.lock.readLock().lock(); 
    ...snip... 
} 

Method B: 
...snip... 
synchronized(this) { 
    ...snip... 
    lock.lock.readLock().unlock(); 
    ...snip... 
} 

您所遇到的问题是从根本上阻止一个同步器同时举行另一死锁。当您在synchronized(this)代码块内呼叫Lock.lock()时,Thread将输入BLOCKING状态,同时继续保持this上的固有锁定。由于线程实际上持有锁以获得内部锁this和内部锁永远不会被释放,所以你有一个死锁。

例如:
线程1进入方法A,获得关于this
线程2进入方法A的锁,不能获得this所以块
线程1获得Readlock
线程1释放this
线程2获得this
Thread2块等待Readlock
Thread1输入方法B,无法获得this所以块
死锁

+0

+1,优秀的解释! – 2011-04-06 03:58:36

另外,别忘了不开心的路径。一定要把你的unlock()放到finally中,否则如果抛出一个异常,unlock()永远不会被调用,最终会导致死锁。