JAVA-17608 Restored AtomicInteger default increment method
This commit is contained in:
parent
8103e7087b
commit
ed51bc4cda
|
@ -1,7 +1,7 @@
|
||||||
package com.baeldung.concurrent.atomic;
|
package com.baeldung.concurrent.atomic;
|
||||||
|
|
||||||
public class SafeCounterWithLock {
|
public class SafeCounterWithLock {
|
||||||
private volatile int counter;
|
private int counter;
|
||||||
|
|
||||||
int getValue() {
|
int getValue() {
|
||||||
return counter;
|
return counter;
|
||||||
|
|
|
@ -10,12 +10,6 @@ public class SafeCounterWithoutLock {
|
||||||
}
|
}
|
||||||
|
|
||||||
void increment() {
|
void increment() {
|
||||||
while(true) {
|
counter.incrementAndGet();
|
||||||
int existingValue = getValue();
|
|
||||||
int newValue = existingValue + 1;
|
|
||||||
if(counter.compareAndSet(existingValue, newValue)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ public class ThreadSafeCounterIntegrationTest {
|
||||||
SafeCounterWithLock safeCounter = new SafeCounterWithLock();
|
SafeCounterWithLock safeCounter = new SafeCounterWithLock();
|
||||||
|
|
||||||
IntStream.range(0, 1000)
|
IntStream.range(0, 1000)
|
||||||
.forEach(count -> service.submit(safeCounter::increment));
|
.forEach(count -> service.execute(safeCounter::increment));
|
||||||
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
shutdownAndAwaitTermination(service);
|
||||||
|
|
||||||
assertEquals(1000, safeCounter.getValue());
|
assertEquals(1000, safeCounter.getValue());
|
||||||
}
|
}
|
||||||
|
@ -29,10 +29,30 @@ public class ThreadSafeCounterIntegrationTest {
|
||||||
SafeCounterWithoutLock safeCounter = new SafeCounterWithoutLock();
|
SafeCounterWithoutLock safeCounter = new SafeCounterWithoutLock();
|
||||||
|
|
||||||
IntStream.range(0, 1000)
|
IntStream.range(0, 1000)
|
||||||
.forEach(count -> service.submit(safeCounter::increment));
|
.forEach(count -> service.execute(safeCounter::increment));
|
||||||
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
shutdownAndAwaitTermination(service);
|
||||||
|
|
||||||
assertEquals(1000, safeCounter.getValue());
|
assertEquals(1000, safeCounter.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void shutdownAndAwaitTermination(ExecutorService pool) {
|
||||||
|
// Disable new tasks from being submitted
|
||||||
|
pool.shutdown();
|
||||||
|
try {
|
||||||
|
// Wait a while for existing tasks to terminate
|
||||||
|
if (!pool.awaitTermination(100, TimeUnit.MILLISECONDS)) {
|
||||||
|
// Cancel currently executing tasks forcefully
|
||||||
|
pool.shutdownNow();
|
||||||
|
// Wait a while for tasks to respond to being cancelled
|
||||||
|
if (!pool.awaitTermination(100, TimeUnit.MILLISECONDS))
|
||||||
|
System.err.println("Pool did not terminate");
|
||||||
|
}
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
// (Re-)Cancel if current thread also interrupted
|
||||||
|
pool.shutdownNow();
|
||||||
|
// Preserve interrupt status
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue