-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathUsingAtomics.java
58 lines (51 loc) · 1.75 KB
/
UsingAtomics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package br.com.leonardoz.features.atomics;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Atomics can be used from the java.util.concurrent.atomic.* package.
*
* An atomic operation is a compound action that totally completes out totally
* fails, not supporting inconsistent values or results during it's execution.
*
* The classes in this package supports atomic operations on single variables,
* having get and set (read and write) methods that behave like a volatile
* variable.
*
* The compareAndSet are commonly used in non-blocking algorithms. They
* basically tries to set a new value to a specified field, and it returns a
* boolean indicating success or not. All atomic, only blocking briefly.
*
* Interesting classes in this package are: AtomicBoolean, AtomicLong,
* AtomicReference<T>, AtomicMarkableReference<T> and
* AtomicReferenceFieldUpdater<T, V>.
*
*
*/
public class UsingAtomics {
/*
* A Counter using AtomicInteger
*/
static class AtomicCounter {
private AtomicInteger atomicInteger = new AtomicInteger(0);
public void increment() {
atomicInteger.incrementAndGet();
}
public void decrement() {
atomicInteger.decrementAndGet();
}
public int get() {
return atomicInteger.get();
}
}
public static void main(String[] args) throws InterruptedException {
var counter = new AtomicCounter();
var cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10_000; i++) {
cachedThreadPool.execute(() -> counter.increment());
}
cachedThreadPool.shutdown();
cachedThreadPool.awaitTermination(4000, TimeUnit.SECONDS);
System.out.println("Result shound be 10000: Actual result is: " + counter.get());
}
}