public void testBody() {
try {
/* Create two initial elements. */
for (int i = 0; i < 2; i++) {
IN in = new IN(dbImpl, null, 1, 1);
inList1.add(in);
}
/*
* Acquire the major latch in preparation for an
* iteration.
*/
inList1.latchMajor();
/* Wait for tester2 to try to acquire the
/* minor latch */
sequencer = 1;
while (sequencer <= 1) {
Thread.yield();
}
/*
* Sequencer is now 2. There should only be
* two elements in the list right now even
* though thread 2 added a third one.
*/
int count = 0;
Iterator iter = inList1.iterator();
while (iter.hasNext()) {
IN in = (IN) iter.next();
count++;
}
assertEquals(2, count);
/*
* Allow thread2 to run again. It will
* add another element and throw control
* back to thread 1.
*/
sequencer++; // now it's 3
while (sequencer <= 3) {
Thread.yield();
}
/*
* Thread2 has exited. Release the major
* latch so that the addedINs can be added
* into the main in set.
*/
inList1.releaseMajorLatch();
/*
* Check that the entry added by tester2 was really
* added.
*/
inList1.latchMajor();
count = 0;
iter = inList1.iterator();
while (iter.hasNext()) {
IN in = (IN) iter.next();
count++;
}
assertEquals(4, count);
inList1.releaseMajorLatch();
} catch (Throwable T) {
T.printStackTrace(System.out);
fail("Thread 1 caught some Throwable: " + T);
}
}
};
JUnitThread tester2 =
new JUnitThread("testMajorMinorLatching-Thread2") {
public void testBody() {
try {
/* Wait for tester1 to start */
while (sequencer < 1) {
Thread.yield();
}
assertEquals(1, sequencer);
/*
* Acquire the minor latch in preparation for some
* concurrent additions.
*/
inList1.add(new IN(dbImpl, null, 1, 1));
sequencer++;
/* Sequencer is now 2. */
while (sequencer < 3) {
Thread.yield();
}
assertEquals(3, sequencer);
/* Add one more element. */
inList1.add(new IN(dbImpl, null, 1, 1));
sequencer++;
} catch (Throwable T) {
T.printStackTrace(System.out);
fail("Thread 2 caught some Throwable: " + T);
}