diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/setvslazyset/Application.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/setvslazyset/Application.java new file mode 100644 index 0000000000..37424147a7 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/setvslazyset/Application.java @@ -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(); + } + +}