1 24 25 package org.objectweb.dream.synchro; 26 27 import org.objectweb.dream.AbstractComponent; 28 import org.objectweb.dream.util.EmptyStringArray; 29 30 36 public class MutexImpl extends AbstractComponent implements Mutex 37 { 38 39 40 protected boolean inuse = false; 41 42 46 49 public void lock() throws InterruptedException 50 { 51 if (Thread.interrupted()) 52 throw new InterruptedException (); 53 synchronized (this) 54 { 55 try 56 { 57 while (inuse) 58 wait(); 59 inuse = true; 60 } 61 catch (InterruptedException ex) 62 { 63 notify(); 64 throw ex; 65 } 66 } 67 } 68 69 72 public boolean timedLock(long msecs) throws InterruptedException 73 { 74 if (Thread.interrupted()) 75 throw new InterruptedException (); 76 synchronized (this) 77 { 78 if (!inuse) 79 { 80 inuse = true; 81 return true; 82 } 83 else if (msecs <= 0) 84 return false; 85 else 86 { 87 long waitTime = msecs; 88 long start = System.currentTimeMillis(); 89 try 90 { 91 for (;;) 92 { 93 wait(waitTime); 94 if (!inuse) 95 { 96 inuse = true; 97 return true; 98 } 99 else 100 { 101 waitTime = msecs - (System.currentTimeMillis() - start); 102 if (waitTime <= 0) 103 return false; 104 } 105 } 106 } 107 catch (InterruptedException ex) 108 { 109 notify(); 110 throw ex; 111 } 112 } 113 } 114 } 115 116 119 public synchronized void unlock() 120 { 121 inuse = false; 122 notify(); 123 } 124 125 129 132 public String [] listFc() 133 { 134 return EmptyStringArray.EMPTY_STRING_ARRAY; 135 } 136 137 } | Popular Tags |