Skip to content
Snippets Groups Projects
Commit b7899407 authored by Thomas Lopatic's avatar Thomas Lopatic
Browse files

Use volatile correctly.

parent 5e1ed2ac
No related branches found
No related tags found
No related merge requests found
......@@ -8,11 +8,11 @@ import java.util.concurrent.locks.ReentrantLock;
*/
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.
......@@ -40,8 +40,11 @@ public class AcknowledgedCounterGenerator extends CounterGenerator
*/
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;
......@@ -68,5 +71,8 @@ public class AcknowledgedCounterGenerator extends CounterGenerator
lock.unlock();
}
}
// write volatile variable to make other threads see changes
limit = limit;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment