Set and Go Operations Example
Example 3.4. If we consider the Example 2.7, “Array Class� given previously, the set and get operations could be implemented as shown below. This is a simplification of the code, ignoring error conditions and exceptions. public boolean set (int index, int value) { boolean result = false; AtomicAction A = new AtomicAction(); A.begin(); // We need to set a WRITE lock as we want to modify the state. if (setlock(new Lock(LockMode.WRITE), 0) == LockResult.GRANTED) { elements[index] = value; if ((value > 0) &&(index > highestIndex highestIndex = index; A.commit(true); result = true; } else A.rollback(); return result; } public int get (int index) // assume -1 means error { AtomicAction A = new AtomicAction(); A.begin(); // We only need a READ lock as the state is unchanged. if (setlock(new Lock(LockMode.READ), 0) == LockResult.GRANTED) { A.commit(true); return elements[index]; } else A.rollback(); return -1; }