Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bfd4cf6

Browse files
committed
VisualSortArray: Changed two counter variables to have less concurrency overhead.
1 parent f6345e8 commit bfd4cf6

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

‎src/io/nayuki/sortalgodemo/visual/VisualSortArray.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.Arrays;
2828
import java.util.Objects;
29+
import java.util.concurrent.atomic.AtomicLong;
2930
import io.nayuki.sortalgodemo.core.AbstractSortArray;
3031

3132

@@ -44,8 +45,8 @@ final class VisualSortArray extends AbstractSortArray {
4445
private volatile boolean isDone;
4546

4647
// Statistics
47-
private volatilelongcomparisonCount;
48-
private volatilelongswapCount;
48+
private AtomicLongcomparisonCount = newAtomicLong();
49+
private AtomicLongswapCount = newAtomicLong();
4950

5051
// Speed regulation
5152
private final double stepsPerSecond;
@@ -68,18 +69,17 @@ public VisualSortArray(int size, double speed) {
6869
stepsPerSecond = speed;
6970
stepsPerCheck = (int)Math.max(Math.min(stepsPerSecond * CHECK_INTERVAL_NS / 1e9, 1_000_000), 1);
7071
state = new int[size];
71-
72-
// Initialize various numbers
73-
comparisonCount = 0;
74-
swapCount = 0;
7572
}
7673

7774

7875
public void finishInitialization() {
7976
if (isInitialized)
8077
throw new IllegalStateException();
8178
isInitialized = true;
79+
8280
isDone = false;
81+
comparisonCount.set(0);
82+
swapCount.set(0);
8383

8484
stepsRemaining = stepsPerCheck;
8585
prevCheckTimeNs = System.nanoTime();
@@ -98,7 +98,7 @@ public int compare(int i, int j) {
9898
setElement(i, ElementState.COMPARING);
9999
setElement(j, ElementState.COMPARING);
100100
regulateSpeed();
101-
comparisonCount++;
101+
comparisonCount.setOpaque(comparisonCount.getPlain() + 1);
102102

103103
setElement(i, ElementState.ACTIVE);
104104
setElement(j, ElementState.ACTIVE);
@@ -113,7 +113,7 @@ public void swap(int i, int j) {
113113
return;
114114
if (Thread.interrupted())
115115
throw new StopException();
116-
swapCount++;
116+
swapCount.setOpaque(swapCount.getPlain() + 1);
117117
setElement(i, ElementState.ACTIVE);
118118
setElement(j, ElementState.ACTIVE);
119119
regulateSpeed();
@@ -147,11 +147,11 @@ public int getState(int index) {
147147
/* After sorting */
148148

149149
public long getComparisonCount() {
150-
return comparisonCount;
150+
return comparisonCount.getOpaque();
151151
}
152152

153153
public long getSwapCount() {
154-
return swapCount;
154+
return swapCount.getOpaque();
155155
}
156156

157157

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /