< prev index next >
test/hotspot/jtreg/runtime/handshake/HandshakeDirectTest.java
Print this page
@@ -22,107 +22,88 @@
*
*/
/*
* @test HandshakeDirectTest
- * @summary This test tries to stress direct handshakes between threads while suspending them.
+ * @summary This test tries to stress direct handshakes between threads.
* @library /testlibrary /test/lib
* @build HandshakeDirectTest
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+SafepointALot -XX:BiasedLockingDecayTime=100000000 -XX:BiasedLockingBulkRebiasThreshold=1000000 -XX:BiasedLockingBulkRevokeThreshold=1000000 HandshakeDirectTest
*/
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.Semaphore;
import java.io.*;
public class HandshakeDirectTest implements Runnable {
static final int WORKING_THREADS = 32;
static final int DIRECT_HANDSHAKES_MARK = 50000;
- static Thread[] _working_threads = new Thread[WORKING_THREADS];
- static java.util.concurrent.Semaphore[] _handshake_sem = new java.util.concurrent.Semaphore[WORKING_THREADS];
- static Object[] _locks = new Object[WORKING_THREADS];
- static boolean[] _is_biased = new boolean[WORKING_THREADS];
- static Thread _suspendresume_thread = new Thread();
- static AtomicInteger _handshake_count = new AtomicInteger(0);
+ static Thread[] workingThreads = new Thread[WORKING_THREADS];
+ static Semaphore[] handshakeSem = new Semaphore[WORKING_THREADS];
+ static Object[] locks = new Object[WORKING_THREADS];
+ static boolean[] isBiased = new boolean[WORKING_THREADS];
+ static AtomicInteger handshakeCount = new AtomicInteger(0);
@Override
public void run() {
int me = Integer.parseInt(Thread.currentThread().getName());
while (true) {
try {
- if (_is_biased[me] == false) {
- _handshake_sem[me].acquire();
- synchronized(_locks[me]) {
- _is_biased[me] = true;
+ if (!isBiased[me]) {
+ handshakeSem[me].acquire();
+ synchronized(locks[me]) {
+ isBiased[me] = true;
}
- _handshake_sem[me].release();
+ handshakeSem[me].release();
}
// Handshake directly some other worker
int handshakee = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS-1);
if (handshakee == me) {
handshakee = handshakee != 0 ? handshakee - 1 : handshakee + 1;
}
- _handshake_sem[handshakee].acquire();
- if (_is_biased[handshakee]) {
+ handshakeSem[handshakee].acquire();
+ if (isBiased[handshakee]) {
// Revoke biased lock
- synchronized(_locks[handshakee]) {
- _handshake_count.incrementAndGet();
+ synchronized(locks[handshakee]) {
+ handshakeCount.incrementAndGet();
}
// Create new lock to be biased
- _locks[handshakee] = new Object();
- _is_biased[handshakee] = false;
+ locks[handshakee] = new Object();
+ isBiased[handshakee] = false;
}
- _handshake_sem[handshakee].release();
- if (_handshake_count.get() >= DIRECT_HANDSHAKES_MARK) {
+ handshakeSem[handshakee].release();
+ if (handshakeCount.get() >= DIRECT_HANDSHAKES_MARK) {
break;
}
} catch(InterruptedException ie) {
+ throw new Error("Unexpected interrupt");
}
}
}
public static void main(String... args) throws Exception {
HandshakeDirectTest test = new HandshakeDirectTest();
// Initialize semaphores
for (int i = 0; i < WORKING_THREADS; i++) {
- _handshake_sem[i] = new java.util.concurrent.Semaphore(1);
+ handshakeSem[i] = new Semaphore(1);
}
// Initialize locks
for (int i = 0; i < WORKING_THREADS; i++) {
- _locks[i] = new Object();
+ locks[i] = new Object();
}
// Fire-up working threads.
for (int i = 0; i < WORKING_THREADS; i++) {
- _working_threads[i] = new Thread(test, Integer.toString(i));
- _working_threads[i].setDaemon(true);
- _working_threads[i].start();
+ workingThreads[i] = new Thread(test, Integer.toString(i));
+ workingThreads[i].setDaemon(true);
+ workingThreads[i].start();
}
- // Fire-up suspend-resume thread
- Thread _suspendresume_thread = new Thread() {
- @Override
- public void run() {
- while (true) {
- int i = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS-1);
- _working_threads[i].suspend();
- try {
- Thread.sleep(1); // sleep for 1 ms
- } catch(InterruptedException ie) {
- }
- _working_threads[i].resume();
- }
- }
- };
- _suspendresume_thread.setDaemon(true);
- _suspendresume_thread.start();
-
// Wait until the desired number of direct handshakes is reached
- while (_handshake_count.get() < DIRECT_HANDSHAKES_MARK) {
- Thread.sleep(10); // sleep for 10ms
- }
+ workingThreads[0].join();
}
}
< prev index next >