Use AtomicInteger instead of unsafe increment on volatile fields

This commit is contained in:
stsypanov
2019-01-18 14:18:41 +02:00
committed by Juergen Hoeller
parent e402a93e41
commit 248d3f8e8b
3 changed files with 30 additions and 26 deletions

View File

@@ -34,6 +34,8 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import java.util.concurrent.atomic.AtomicInteger;
/**
* A {@code SpelExpression} represents a parsed (valid) expression that is ready to be
* evaluated in a specified context. An expression can be evaluated standalone or in a
@@ -69,7 +71,7 @@ public class SpelExpression implements Expression {
// Count of many times as the expression been interpreted - can trigger compilation
// when certain limit reached
private volatile int interpretedCount = 0;
private final AtomicInteger interpretedCount = new AtomicInteger(0);
// The number of times compilation was attempted and failed - enables us to eventually
// give up trying to compile it when it just doesn't seem to be possible.
@@ -124,7 +126,7 @@ public class SpelExpression implements Expression {
catch (Throwable ex) {
// If running in mixed mode, revert to interpreted
if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
this.interpretedCount = 0;
this.interpretedCount.set(0);
this.compiledAst = null;
}
else {
@@ -159,7 +161,7 @@ public class SpelExpression implements Expression {
catch (Throwable ex) {
// If running in mixed mode, revert to interpreted
if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
this.interpretedCount = 0;
this.interpretedCount.set(0);
this.compiledAst = null;
}
else {
@@ -186,7 +188,7 @@ public class SpelExpression implements Expression {
catch (Throwable ex) {
// If running in mixed mode, revert to interpreted
if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
this.interpretedCount = 0;
this.interpretedCount.set(0);
this.compiledAst = null;
}
else {
@@ -221,7 +223,7 @@ public class SpelExpression implements Expression {
catch (Throwable ex) {
// If running in mixed mode, revert to interpreted
if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
this.interpretedCount = 0;
this.interpretedCount.set(0);
this.compiledAst = null;
}
else {
@@ -251,7 +253,7 @@ public class SpelExpression implements Expression {
catch (Throwable ex) {
// If running in mixed mode, revert to interpreted
if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
this.interpretedCount = 0;
this.interpretedCount.set(0);
this.compiledAst = null;
}
else {
@@ -286,7 +288,7 @@ public class SpelExpression implements Expression {
catch (Throwable ex) {
// If running in mixed mode, revert to interpreted
if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
this.interpretedCount = 0;
this.interpretedCount.set(0);
this.compiledAst = null;
}
else {
@@ -314,7 +316,7 @@ public class SpelExpression implements Expression {
catch (Throwable ex) {
// If running in mixed mode, revert to interpreted
if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
this.interpretedCount = 0;
this.interpretedCount.set(0);
this.compiledAst = null;
}
else {
@@ -351,7 +353,7 @@ public class SpelExpression implements Expression {
catch (Throwable ex) {
// If running in mixed mode, revert to interpreted
if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
this.interpretedCount = 0;
this.interpretedCount.set(0);
this.compiledAst = null;
}
else {
@@ -473,17 +475,17 @@ public class SpelExpression implements Expression {
* @param expressionState the expression state used to determine compilation mode
*/
private void checkCompile(ExpressionState expressionState) {
this.interpretedCount++;
this.interpretedCount.incrementAndGet();
SpelCompilerMode compilerMode = expressionState.getConfiguration().getCompilerMode();
if (compilerMode != SpelCompilerMode.OFF) {
if (compilerMode == SpelCompilerMode.IMMEDIATE) {
if (this.interpretedCount > 1) {
if (this.interpretedCount.get() > 1) {
compileExpression();
}
}
else {
// compilerMode = SpelCompilerMode.MIXED
if (this.interpretedCount > INTERPRETED_COUNT_THRESHOLD) {
if (this.interpretedCount.get() > INTERPRETED_COUNT_THRESHOLD) {
compileExpression();
}
}
@@ -524,7 +526,7 @@ public class SpelExpression implements Expression {
*/
public void revertToInterpreted() {
this.compiledAst = null;
this.interpretedCount = 0;
this.interpretedCount.set(0);
this.failedAttempts = 0;
}