Skip to content

Commit

Permalink
Use volatile correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
uncle-betty committed Sep 21, 2015
1 parent f474adb commit 09dadfc
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
*/
public class AcknowledgedCounterGenerator extends CounterGenerator
{
private static final int WINDOW_SIZE = 10000;
private static final int WINDOW_SIZE = 1000000;

private ReentrantLock lock;
private boolean[] window;
private int limit;
private final ReentrantLock lock;
private final boolean[] window;
private volatile int limit;

/**
* Create a counter that starts at countstart.
Expand Down Expand Up @@ -40,8 +40,11 @@ public int lastInt()
*/
public void acknowledge(int value)
{
// read volatile variable to see other threads' changes
limit = limit;

if (value > limit + WINDOW_SIZE) {
throw new RuntimeException("This should be a different exception.");
throw new RuntimeException("Too many unacknowledged insertion keys.");
}

window[value % WINDOW_SIZE] = true;
Expand All @@ -68,5 +71,8 @@ public void acknowledge(int value)
lock.unlock();
}
}

// write volatile variable to make other threads see changes
limit = limit;
}
}

0 comments on commit 09dadfc

Please sign in to comment.