1 50 package org.apache.avalon.excalibur.pool; 51 52 63 class Mutex 64 { 65 private long m_tokens; 66 67 73 public Mutex() 74 { 75 m_tokens = 1; 76 } 77 78 public synchronized void acquire() 79 throws InterruptedException 80 { 81 if( Thread.interrupted() ) throw new InterruptedException (); 83 84 while( 0 >= m_tokens ) 86 { 87 wait(); 88 } 89 m_tokens--; 90 } 91 92 public synchronized void release() 93 { 94 m_tokens++; 95 notify(); 96 } 97 98 public synchronized boolean attempt( final long msecs ) 99 throws InterruptedException 100 { 101 if( Thread.interrupted() ) throw new InterruptedException (); 102 103 if( m_tokens > 0 ) 104 { 105 m_tokens--; 106 return true; 107 } 108 else 109 { 110 final long start = System.currentTimeMillis(); 111 long wait = msecs; 112 113 while( wait > 0 ) 114 { 115 wait( wait ); 116 117 if( m_tokens > 0 ) 118 { 119 m_tokens--; 120 return true; 121 } 122 else 123 { 124 wait = msecs - ( System.currentTimeMillis() - start ); 125 } 126 } 127 128 return false; 129 } 130 } 131 } 132 | Popular Tags |