| 1 package com.daffodilwoods.daffodildb.utils; 2 3 import com.daffodilwoods.database.resource.DException; 4 import java.util.*; 5 6 public class Lock{ 7 public int locker; 8 public int lockCount; 9 ArrayList waitList; 10 public Lock() throws DException{ 11 waitList = new ArrayList(); 12 } 13 14 18 public synchronized void lock(int i) throws DException { 19 while(true){ 20 if(locker==0){ 21 locker=i; 22 waitList.remove(new Integer (i)); 23 return; 24 } 25 else if(locker==i && waitList.size() < 1 ) { 26 lockCount++; 27 return; 28 } 29 if( !waitList.contains( new Integer (i) ) ) 30 waitList.add(new Integer (i)); 31 32 try{ 33 wait(); 34 }catch(Exception e ){e.printStackTrace();}; 35 } 36 } 37 38 public String showWaitList()throws DException{ 39 StringBuffer sb = new StringBuffer (); 40 sb.append(this.hashCode()); 41 if( waitList.size() > 0 ){ 42 sb.append(" : "); 43 for (int i = 0; i < waitList.size(); i++) { 44 sb.append( waitList.get(i) ); 45 sb.append(", "); 46 } 47 } 48 return sb.toString(); 49 } 50 51 public synchronized void unLock() throws DException { 52 if(lockCount>0) 53 lockCount--; 54 else{ 55 locker=0; 56 notifyAll(); 57 } 58 } 59 } 60 | Popular Tags |