27package jau.test.util.parallel.locks;
31import jau.test.util.parallel.locks.impl.SingletonInstanceFileLock;
32import jau.test.util.parallel.locks.impl.SingletonInstanceServerSocket;
36 protected static final boolean DEBUG =
true;
64 this.poll_ms = Math.max(10, poll_ms);
73 public synchronized void lock() throws RuntimeException {
80 }
catch (
final RuntimeException ie ) {
81 throw new RuntimeException(ie);
86 public synchronized boolean tryLock(
long maxwait)
throws RuntimeException {
90 final long t0 = System.currentTimeMillis();
94 final long t1 = System.currentTimeMillis();
98 final long t2 = System.currentTimeMillis();
99 System.err.println(
infoPrefix(t2)+
" +++ "+
getName()+
" - Locked within "+(t2-t0)+
" ms, "+(i+1)+
" attempts");
103 if(
DEBUG && 0==i ) {
104 System.err.println(
infoPrefix(System.currentTimeMillis())+
" III "+
getName()+
" - Wait for lock");
106 Thread.sleep(poll_ms);
107 maxwait -= System.currentTimeMillis()-t1;
109 }
while ( 0 < maxwait ) ;
110 }
catch (
final InterruptedException ie ) {
111 final long t2 = System.currentTimeMillis();
112 throw new RuntimeException(
infoPrefix(t2)+
" EEE (1) "+
getName()+
" - couldn't get lock within "+(t2-t0)+
" ms, "+i+
" attempts", ie);
115 final long t2 = System.currentTimeMillis();
116 System.err.println(
infoPrefix(t2)+
" +++ EEE (2) "+
getName()+
" - couldn't get lock within "+(t2-t0)+
" ms, "+i+
" attempts");
123 public void unlock() throws RuntimeException {
124 final long t0 = System.currentTimeMillis();
128 final long t2 = System.currentTimeMillis();
129 System.err.println(
infoPrefix(t2)+
" --- "+
getName()+
" - Unlock "+ ( locked ?
"failed" :
"ok" ) +
" within "+(t2-t0)+
" ms");
141 return "SLOCK [T "+Thread.currentThread().getName()+
" @ "+currentMillis+
" ms";
144 return infoPrefix(System.currentTimeMillis());
147 private final long poll_ms;
148 private boolean locked =
false;
synchronized void lock()
Blocking until the lock is acquired by this Thread or TIMEOUT is reached.
abstract String getName()
abstract boolean unlockImpl()
void unlock()
Release the lock.
abstract boolean tryLockImpl()
final long getPollPeriod()
SingletonInstance(final long poll_ms)
static SingletonInstance createServerSocket(final long poll_ms, final int portNumber)
A user shall use ephemeral ports:
static SingletonInstance createFileLock(final long poll_ms, final String lockFileBasename)
synchronized boolean tryLock(long maxwait)
Blocking until the lock is acquired by this Thread or maxwait in ms is reached.
synchronized boolean isLocked()
Query if locked.
static final boolean DEBUG
String infoPrefix(final long currentMillis)
static SingletonInstance createFileLock(final long poll_ms, final File lockFile)
Specifying a thread blocking lock implementation.
static final long TIMEOUT
The TIMEOUT for lock() in ms, defaults to DEFAULT_TIMEOUT.