Skip to content

Instantly share code, notes, and snippets.

@danielshaya
Last active August 29, 2015 14:27
Show Gist options
  • Save danielshaya/24553df9e96ee2ae0e73 to your computer and use it in GitHub Desktop.
Save danielshaya/24553df9e96ee2ae0e73 to your computer and use it in GitHub Desktop.
import org.junit.Test;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockVsSync {
private static final boolean COORDINATED_OMISSION = Boolean.getBoolean("coordinatedOmission");
//Either run testing Lock or testing synchronized
private static final boolean IS_LOCK = Boolean.getBoolean("isLock");
private static final int NUM_THREADS = Integer.getInteger("numThreads");
@Test
public void test() throws InterruptedException {
Lock lock = new ReentrantLock();
for (int t = 0; t < NUM_THREADS; t++) {
if (t == 0) {
//Set the first thread as the master which will be measured
//The other threads are only to cause contention
Runner r = new Runner(lock, true);
r.start();
} else {
Runner r = new Runner(lock, false);
r.start();
}
}
synchronized(this){
//Hold the main thread from completing
wait();
}
}
private void testLock(Lock rlock) {
rlock.lock();
try {
for (int i = 0; i < 2; i++) {
double x = 10 / 4.5 + i;
}
} finally {
rlock.unlock();
}
}
private synchronized void testSync() {
for (int i = 0; i < 2; i++) {
double x = 10 / 4.5 + i;
}
}
class Runner extends Thread {
private Lock lock;
private boolean master;
public Runner(Lock lock, boolean master) {
this.lock = lock;
this.master = master;
}
@Override
public void run() {
Histogram histogram = null;
if (master)
histogram = new Histogram();
long rate = 1000;//expect 1 every microsecond
long now =0;
for (int i = -10000; i < 200_000_000; i++) {
if(i==0){
now = System.nanoTime();
} else if(i>0){
if(!COORDINATED_OMISSION) {
now += rate;
while(System.nanoTime() < now)
;
}else
now = System.nanoTime();
}
if(IS_LOCK)
testLock(lock);
else
testSync();
if(i>=0 && master){
histogram.sample(System.nanoTime() - now);
}
}
if (master) {
System.out.println(histogram.toMicrosFormat());
System.exit(0);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment