28package com.jogamp.common.util.locks;
30import java.io.IOException;
32import org.junit.Assert;
33import org.junit.BeforeClass;
35import org.junit.FixMethodOrder;
36import org.junit.runners.MethodSorters;
38import com.jogamp.common.util.InterruptSource;
39import com.jogamp.junit.util.JunitTracer;
40import com.jogamp.junit.util.SingletonJunitCase;
42@FixMethodOrder(MethodSorters.NAME_ASCENDING)
46 public static final long SINGLE_INSTANCE_LOCK_POLL = 100;
59 Assert.assertTrue(
"Could not lock single instance: "+singletonInstance.
getName(), singletonInstance.
tryLock(SINGLE_INSTANCE_LOCK_TO));
61 singletonInstance.
unlock();
66 Assert.assertTrue(
"Could not lock single instance: "+singletonInstance.
getName(), singletonInstance.
tryLock(SINGLE_INSTANCE_LOCK_TO));
68 Assert.assertFalse(
"Could lock 2nd instance: "+instanceTwo.
getName(), instanceTwo.
tryLock(1000));
70 singletonInstance.
unlock();
73 private Thread startLockUnlockOffThread(
final int i) {
77 System.err.println(Thread.currentThread().getName()+
" LOCK try ..");
78 Assert.assertTrue(Thread.currentThread().getName()+
" - Could not lock instance: "+myLock.
getName(), myLock.
tryLock(1000));
79 System.err.println(Thread.currentThread().getName()+
" LOCK ON");
82 }
catch (
final InterruptedException e) { }
84 System.err.println(Thread.currentThread().getName()+
" LOCK OFF");
93 Assert.assertTrue(
"Could not lock single instance: "+singletonInstance.
getName(), singletonInstance.
tryLock(SINGLE_INSTANCE_LOCK_TO));
94 final Thread t1 = startLockUnlockOffThread(1);
95 final Thread t2 = startLockUnlockOffThread(2);
98 singletonInstance.
unlock();
99 while(t1.isAlive() || t2.isAlive()) {
104 public static void main(
final String args[])
throws IOException, InterruptedException {
106 org.junit.runner.JUnitCore.
main(tstname);
java.lang.Thread specialization implementing InterruptSource to track java.lang.Thread#interrupt() ca...
abstract String getName()
synchronized boolean tryLock(long maxwait)
Blocking until the lock is acquired by this Thread or maxwait in ms is reached.
static SingletonInstance createServerSocket(final long poll_ms, final int portNumber)
A user shall use ephemeral ports:
void unlock()
Release the lock.
void test02_2ndInstanceLockTimeout()
static void main(final String args[])
static void oneTimeSetUp()
void testOffthreadLockUnlock()
static final int SINGLE_INSTANCE_LOCK_PORT
static final long SINGLE_INSTANCE_LOCK_TO
Interface exposing java.lang.Thread#interrupt() source, intended for java.lang.Thread specializations...