Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/main
.gitignore
LICENSE
README.md
pom.xml

README.md

AtomicInteger Example

An AtomicInteger is used in applications such as atomically incremented counters.

Short sample code:

public class AtomicIntegerExample {
    
    private final ExecutorService execService = Executors.newFixedThreadPool(100);
    private final AtomicInteger counter = new AtomicInteger(100_000);
    
    public static void main(String[] args) {
        AtomicIntegerExample exp = new AtomicIntegerExample();
        exp.start();
    }
    
    private void start() {
        for (int i = 0; i < 10_000; i++) {
            execService.execute(sendRequest(i+1));
        }
    }
    
    private Runnable sendRequest(int i) {
        return () -> {
            int id = counter.incrementAndGet();  
            System.out.printf("Request number: %5s  |  OrderId: [%s]  |  %s\n", i, id, LocalTime.now());
        };
    }
}