Skip to content

Commit 0db1661

Browse files
committed
Replaced volatile with an updater
1 parent 988154e commit 0db1661

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/main/groovy/groovyx/gpars/agent/AgentCore.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.concurrent.ConcurrentLinkedQueue;
2727
import java.util.concurrent.ExecutorService;
2828
import java.util.concurrent.Executors;
29-
import java.util.concurrent.atomic.AtomicBoolean;
29+
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
3030

3131
/**
3232
* @author Vaclav Pech
@@ -75,7 +75,10 @@ public final void attachToThreadPool(final ExecutorService threadPool) {
7575
/**
7676
* Indicates, whether there's an active thread handling a message inside the agent's body
7777
*/
78-
private final AtomicBoolean active = new AtomicBoolean(false);
78+
private volatile int active = PASSIVE;
79+
private static final AtomicIntegerFieldUpdater<AgentCore> activeUpdater = AtomicIntegerFieldUpdater.newUpdater(AgentCore.class, "active");
80+
private static final int PASSIVE = 0;
81+
private static final int ACTIVE = 1;
7982

8083
/**
8184
* Adds the message to the agent\s message queue
@@ -108,7 +111,7 @@ public final void leftShift(final Object message) {
108111
* Schedules processing of a next message, if there are some and if there isn't an active thread handling a message at the moment
109112
*/
110113
void schedule() {
111-
if (!queue.isEmpty() && active.compareAndSet(false, true)) {
114+
if (!queue.isEmpty() && activeUpdater.compareAndSet(this, PASSIVE, ACTIVE)) {
112115
threadPool.submit(this);
113116
}
114117
}
@@ -124,7 +127,7 @@ public void run() {
124127
} catch (Exception e) {
125128
registerError(e);
126129
} finally {
127-
active.set(false);
130+
activeUpdater.set(this, PASSIVE);
128131
schedule();
129132
}
130133
}

0 commit comments

Comments
 (0)