Add AtomicInteger.set() and .lazySet() usage example (#12099)

This commit is contained in:
kpentaris 2022-05-07 06:27:31 +03:00 committed by GitHub
parent f62a3ee5ca
commit 44bb031933
1 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package com.baeldung.setvslazyset;
import java.util.concurrent.atomic.AtomicInteger;
public class Application {
AtomicInteger atomic = new AtomicInteger(0);
public static void main(String[] args) {
Application app = new Application();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
//app.atomic.set(i);
app.atomic.lazySet(i);
System.out.println("Set: " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
synchronized (app.atomic) {
int counter = app.atomic.get();
System.out.println("Get: " + counter);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}).start();
}
}