1 13 14 package com.sun.corba.se.impl.orbutil.concurrent; 15 16 108 109 public class Mutex implements Sync { 110 111 112 protected boolean inuse_ = false; 113 114 public void acquire() throws InterruptedException { 115 if (Thread.interrupted()) throw new InterruptedException (); 116 synchronized(this) { 117 try { 118 while (inuse_) wait(); 119 inuse_ = true; 120 } 121 catch (InterruptedException ex) { 122 notify(); 123 throw ex; 124 } 125 } 126 } 127 128 public synchronized void release() { 129 inuse_ = false; 130 notify(); 131 } 132 133 134 public boolean attempt(long msecs) throws InterruptedException { 135 if (Thread.interrupted()) throw new InterruptedException (); 136 synchronized(this) { 137 if (!inuse_) { 138 inuse_ = true; 139 return true; 140 } 141 else if (msecs <= 0) 142 return false; 143 else { 144 long waitTime = msecs; 145 long start = System.currentTimeMillis(); 146 try { 147 for (;;) { 148 wait(waitTime); 149 if (!inuse_) { 150 inuse_ = true; 151 return true; 152 } 153 else { 154 waitTime = msecs - (System.currentTimeMillis() - start); 155 if (waitTime <= 0) 156 return false; 157 } 158 } 159 } 160 catch (InterruptedException ex) { 161 notify(); 162 throw ex; 163 } 164 } 165 } 166 } 167 168 } 169 170 | Popular Tags |