final ReadWriteUpgradeLockManager manager = new ReadWriteUpgradeLockManager(sLogger, 100);
int concurrentThreads = 7;
int threads = CONCURRENT_TESTS * concurrentThreads;
final RendezvousBarrier end = new RendezvousBarrier("end", threads + 1, TIMEOUT, sLogger);
sLogger.logInfo("\n\nStarting "+threads+" threads\n\n");
for (int i = 0; i < CONCURRENT_TESTS; i++) {
final int cnt = i;
System.out.print(".");
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
manager.readLock(owner1, res1);
manager.readLock(owner1, res2);
manager.upgradeLock(owner1, res3);
manager.writeLock(owner1, res3);
} catch (LockException ie) {
System.out.print("-");
} finally {
manager.releaseAll(owner1);
end.call();
}
}
}, "Thread #1");
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
manager.readLock(owner2, res1);
manager.readLock(owner2, res2);
manager.upgradeLock(owner2, res3);
manager.writeLock(owner2, res3);
} catch (LockException ie) {
System.out.print("-");
} finally {
manager.releaseAll(owner2);
end.call();
}
}
}, "Thread #2");
Thread t3 = new Thread(new Runnable() {
public void run() {
try {
manager.startGlobalTimeout(owner3, 10 + cnt);
manager.readLock(owner3, res1);
manager.readLock(owner3, res2);
manager.upgradeLock(owner3, res3);
manager.writeLock(owner3, res3);
} catch (LockException le) {
if (le.getCode() == LockException.CODE_TIMED_OUT) {
System.out.print("*");
} else {
System.out.print("-");
}
} finally {
manager.releaseAll(owner3);
end.call();
}
}
}, "Thread #3");
Thread t4 = new Thread(new Runnable() {
public void run() {
try {
manager.readLock(owner4, res1);
manager.readLock(owner4, res2);
manager.upgradeLock(owner4, res3);
manager.writeLock(owner4, res3);
} catch (LockException le) {
System.out.print("-");
} finally {
manager.releaseAll(owner4);
end.call();
}
}
}, "Thread #4");
Thread deadlock1 = new Thread(new Runnable() {
public void run() {
try {
manager.writeLock(owner5, res2);
manager.writeLock(owner5, res1);
} catch (LockException le) {
assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
System.out.print("-");
} finally {
manager.releaseAll(owner5);
end.call();
}
}
}, "Deadlock1 Thread");
Thread deadlock2 = new Thread(new Runnable() {
public void run() {
try {
manager.readLock(owner6, res1);
manager.readLock(owner6, res2);
} catch (LockException le) {
assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
System.out.print("-");
} finally {
manager.releaseAll(owner6);
end.call();
}
}
}, "Deadlock1 Thread");
Thread reader = new Thread(new Runnable() {
public void run() {
try {
manager.readLock("reader", res1);
manager.readLock("reader", res2);
manager.readLock("reader", res3);
} catch (LockException ie) {
System.out.print("-");
} finally {
manager.releaseAll("reader");
end.call();
}
}
}, "Reader Thread");
t4.start();
t3.start();
reader.start();
t1.start();
deadlock2.start();
t2.start();
deadlock1.start();
}
// wait until all threads have really terminated
end.meet();
}