Last active
August 29, 2015 13:57
-
-
Save asafjaffi/9505301 to your computer and use it in GitHub Desktop.
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
import java.util.concurrent.BrokenBarrierException; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.CyclicBarrier; | |
public class TestItAll { | |
CyclicBarrier cb = new CyclicBarrier(8); | |
private volatile boolean working = true; | |
public static void main(String[] args) throws InterruptedException { | |
TestItAll obj = new TestItAll(); | |
Pacman[] pacmans = new Pacman[] { new PacmanSync(), new PacmanLocked(), | |
new PacmanRWLocked(), new PacmanStampedLocked(), new PacmanUnlocked() }; | |
for (Pacman pacman : pacmans) { | |
obj.testPacman(pacman); | |
Thread.sleep(1000); | |
} | |
} | |
private void testPacman(Pacman pacman) { | |
CountDownLatch cdl = new CountDownLatch(7); | |
Results r = new Results(); | |
for (int i = 0; i < 1; i++) { | |
new Thread(new WriterP(i, r, pacman, cdl)).start(); | |
} | |
for (int i = 0; i < 6; i++) { | |
new Thread(new ReaderP(i, r, pacman, cdl)).start(); | |
} | |
try { | |
cb.await(); | |
Thread.sleep(10000); | |
working = false; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
try { | |
cdl.await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
long readSum = 0; | |
for (long c : r.rc) { | |
readSum+= c; | |
} | |
System.out.println(pacman.getClass().getSimpleName()); | |
System.out.println(String.format("Reads: %,d", readSum)); | |
System.out.println(String.format("Writes: %,d", r.wc[0])); | |
// System.out.println("Writer2: " + r.wc[1]); | |
System.out.println("-------"); | |
working = true; | |
} | |
private class WriterP implements Runnable { | |
private final Pacman p; | |
private final CountDownLatch cdl; | |
private final Results r; | |
private final int i; | |
public WriterP(int i, Results r, Pacman p, CountDownLatch cdl) { | |
this.p = p; | |
this.cdl = cdl; | |
this.i = i; | |
this.r = r; | |
} | |
@Override | |
public void run() { | |
try { | |
cb.await(); | |
while (working) { | |
p.move(1, 1); | |
r.wc[i]++; | |
} | |
cdl.countDown(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (BrokenBarrierException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private class ReaderP implements Runnable { | |
private final Pacman p; | |
private final CountDownLatch cdl; | |
private final Results r; | |
private final int i; | |
public ReaderP(int i, Results r, Pacman p, CountDownLatch cdl) { | |
this.p = p; | |
this.cdl = cdl; | |
this.r = r; | |
this.i = i; | |
} | |
@Override | |
public void run() { | |
long sum = 0; | |
try { | |
cb.await(); | |
while (working) { | |
sum += p.sumOfPosition(); | |
r.rc[i]++; | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (BrokenBarrierException e) { | |
e.printStackTrace(); | |
} | |
cdl.countDown(); | |
} | |
} | |
class Results { | |
public long[] wc = new long[8]; | |
public long[] rc = new long[8]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment