@Override protected final void setUp() throws Exception { boolean fair = new Random().nextBoolean(); monitor = new Monitor(fair); tearDownStack.addTearDown(thread1 = new TestThread<>(monitor, "TestThread #1")); tearDownStack.addTearDown(thread2 = new TestThread<>(monitor, "TestThread #2")); }
public TestThread(L lockLikeObject, String threadName) { super(threadName); this.lockLikeObject = checkNotNull(lockLikeObject); start(); }
public final void testMutualExclusion() throws Exception { thread1.callAndAssertReturns(enter()); thread2.callAndAssertBlocks(enter()); thread1.callAndAssertReturns(leave()); thread2.assertPriorCallReturns(enter()); }
public final void testEnterIf_initiallyTrue() throws Exception { TestGuard guard = new TestGuard(true); thread1.callAndAssertReturns(true, enterIf(), guard); thread2.callAndAssertBlocks(enter()); }
new Thread() { @Override public void run() { TestThread t1= new TestThread("1"); TestThread t2= new TestThread("2"); try { t1.start(); t1.join(); t2.start(); t2.join(); } catch (InterruptedException e) { e.printStackTrace(); } } }.start();
public static void main(String[] args) throws Exception { TestThread t1 = new TestThread(); long startTime = System.currentTimeMillis(); t1.start(); long currentTime = System.currentTimeMillis(); while (currentTime - startTime < 5000) { // you can decide the desired interval sleep(1000); // sleep some time currentTime = System.currentTimeMillis(); System.out.println(currentTime); //print this to ensure that the program is still running } t1.interrupt(); //interrupt the thread }
final TestThread t2= new TestThread("2"); TestThread t1= new TestThread("1") { @Override public void run() { super.run(); //finish t1 work t2.start(); // start t2 work } }; t1.start();
public final void testWaitFor_initiallyFalse() throws Exception { TestGuard guard = new TestGuard(false); thread1.callAndAssertReturns(enter()); thread1.callAndAssertWaits(waitFor(), guard); monitor.enter(); guard.setSatisfied(true); monitor.leave(); thread1.assertPriorCallReturns(waitFor()); }
private void checkSystemStateMethods(int enterCount) throws Exception { thread1.callAndAssertReturns(enterCount != 0, "isOccupied"); thread1.callAndAssertReturns(enterCount != 0, "isOccupiedByCurrentThread"); thread1.callAndAssertReturns(enterCount, "getOccupiedDepth"); thread2.callAndAssertReturns(enterCount != 0, "isOccupied"); thread2.callAndAssertReturns(false, "isOccupiedByCurrentThread"); thread2.callAndAssertReturns(0, "getOccupiedDepth"); }
public final void testEnterWhen_initiallyFalse() throws Exception { TestGuard guard = new TestGuard(false); thread1.callAndAssertWaits(enterWhen(), guard); monitor.enter(); guard.setSatisfied(true); monitor.leave(); thread1.assertPriorCallReturns(enterWhen()); }
private Object invokeMethod(String methodName, Object... arguments) throws Exception { return getMethod(methodName, arguments).invoke(lockLikeObject, arguments); }
public final void testWaitFor_withoutEnter() throws Exception { TestGuard guard = new TestGuard(true); thread1.callAndAssertThrows(IllegalMonitorStateException.class, waitFor(), guard); }
public static void main(String[] args) throws InterruptedException { TestThread testThread = new TestThread(); Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces(); for(Thread s: map.keySet()){ System.out.println(s + Arrays.toString(map.get(s))); } System.out.println("-----------------------------------------"); testThread.start(); testThread.join(); }
public final void testEnterIf_alreadyOccupied() throws Exception { TestGuard guard = new TestGuard(true); thread2.callAndAssertReturns(enter()); thread1.callAndAssertBlocks(enterIf(), guard); thread2.callAndAssertReturns(leave()); thread1.assertPriorCallReturns(true, enterIf()); }
public final void testWaitFor_initiallyFalse() throws Exception { TestGuard guard = new TestGuard(false); thread1.callAndAssertReturns(enter()); thread1.callAndAssertWaits(waitFor(), guard); monitor.enter(); guard.setSatisfied(true); monitor.leave(); thread1.assertPriorCallReturns(waitFor()); }
public final void testTryEnter() throws Exception { thread1.callAndAssertReturns(true, tryEnter()); thread2.callAndAssertReturns(false, tryEnter()); thread1.callAndAssertReturns(true, tryEnter()); thread2.callAndAssertReturns(false, tryEnter()); thread1.callAndAssertReturns(leave()); thread2.callAndAssertReturns(false, tryEnter()); thread1.callAndAssertReturns(leave()); thread2.callAndAssertReturns(true, tryEnter()); }
public final void testTryEnterIf_initiallyTrue() throws Exception { TestGuard guard = new TestGuard(true); thread1.callAndAssertReturns(true, tryEnterIf(), guard); thread2.callAndAssertBlocks(enter()); }
public final void testEnterWhen_initiallyFalse() throws Exception { TestGuard guard = new TestGuard(false); thread1.callAndAssertWaits(enterWhen(), guard); monitor.enter(); guard.setSatisfied(true); monitor.leave(); thread1.assertPriorCallReturns(enterWhen()); }
private Object invokeMethod(String methodName, Object... arguments) throws Exception { return getMethod(methodName, arguments).invoke(lockLikeObject, arguments); }