Skip to content

Commit ce28f07

Browse files
committed
updates
1 parent 4c1584c commit ce28f07

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

  • de.vogella.concurrency.nonblocking.counter/src/de/vogella/concurrency/nonblocking/counter

‎de.vogella.concurrency.nonblocking.counter/src/de/vogella/concurrency/nonblocking/counter/Counter.java‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
import java.util.concurrent.atomic.AtomicInteger;
44

55
public class Counter {
6-
private AtomicInteger value = new AtomicInteger();
7-
public int getValue(){
6+
private AtomicInteger value = new AtomicInteger();
7+
8+
public int getValue() {
89
return value.get();
910
}
10-
public int increment(){
11+
12+
public int increment() {
1113
return value.incrementAndGet();
1214
}
13-
14-
// Alternative implementation as increment but just make the
15+
16+
// Alternative implementation as increment but just make the
1517
// implementation explicit
16-
public int incrementLongVersion(){
18+
public int incrementLongVersion() {
1719
int oldValue = value.get();
18-
while (value.compareAndSet(value.get(),oldValue+1)){
19-
oldValue = value.get();
20+
while (!value.compareAndSet(value.get(), oldValue + 1)) {
21+
oldValue = value.get();
2022
}
21-
return oldValue+1;
23+
return oldValue + 1;
2224
}
23-
25+
2426
}

0 commit comments

Comments
 (0)