Created
May 2, 2012 19:08
-
-
Save vikrum/2579362 to your computer and use it in GitHub Desktop.
Locks On Locks On Locks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.CountDownLatch; | |
import org.apache.commons.lang3.time.StopWatch; | |
public class LockOnLocksOnLocks { | |
/** | |
* @param args | |
* @throws InterruptedException | |
*/ | |
public static void main(String[] args) throws InterruptedException { | |
System.err.print("-> "); | |
int simultaneous = 2028; | |
CountDownLatch readySignal = new CountDownLatch(simultaneous); | |
CountDownLatch startSignal = new CountDownLatch(1); | |
CountDownLatch doneSignal = new CountDownLatch(simultaneous); | |
StopWatch stopWatch = new StopWatch(); | |
int count = 1000000; | |
for(int i = 0; i < simultaneous; i++) { | |
try { | |
// -> 0:04:34.254 (274s) | |
// new Thread(new GlobalLockWorker(readySignal, startSignal, doneSignal, i, count)).start(); | |
// -> 0:01:32.064 (92s) | |
new Thread(new StrippedLockWorker(readySignal, startSignal, doneSignal, i, count)).start(); | |
} | |
catch(Throwable t) { | |
t.printStackTrace(); | |
System.err.println(i); | |
} | |
} | |
readySignal.await(); | |
stopWatch.start(); | |
startSignal.countDown(); | |
doneSignal.await(); | |
stopWatch.stop(); | |
System.err.println(stopWatch); | |
} | |
} | |
class GlobalLockWorker implements Runnable { | |
private static final Object globalLock = new Object(); | |
private static final Map<Integer, Boolean> hash = new HashMap<Integer, Boolean>(); | |
private final CountDownLatch readySignal; | |
private final CountDownLatch startSignal; | |
private final CountDownLatch doneSignal; | |
private int id; | |
private int count; | |
public GlobalLockWorker(CountDownLatch readySignal, CountDownLatch startSignal, CountDownLatch doneSignal, int id, int count) { | |
this.readySignal = readySignal; | |
this.startSignal = startSignal; | |
this.doneSignal = doneSignal; | |
this.id = id; | |
this.count = count; | |
} | |
@Override | |
public void run() { | |
try { | |
readySignal.countDown(); | |
startSignal.await(); | |
for(int i = 0; i < count; i++) { | |
synchronized (globalLock) { | |
hash.put(new Integer(id), true); | |
} | |
} | |
doneSignal.countDown(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class StrippedLockWorker implements Runnable { | |
private static final Map<Integer, Boolean> hash = new ConcurrentHashMap<Integer, Boolean>(); | |
private final CountDownLatch readySignal; | |
private final CountDownLatch startSignal; | |
private final CountDownLatch doneSignal; | |
private int id; | |
private int count; | |
public StrippedLockWorker(CountDownLatch readySignal, CountDownLatch startSignal, CountDownLatch doneSignal, int id, int count) { | |
this.readySignal = readySignal; | |
this.startSignal = startSignal; | |
this.doneSignal = doneSignal; | |
this.id = id; | |
this.count = count; | |
} | |
@Override | |
public void run() { | |
try { | |
readySignal.countDown(); | |
startSignal.await(); | |
for(int i = 0; i < count; i++) { | |
hash.put(new Integer(id), true); | |
} | |
doneSignal.countDown(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment