Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment AtomicInteger twice?

I need to increment AtomicInteger twice, like: ++i; ++i; By the endless ciycle, i want to increment counter twice and check it on the parity. But i'm always getting the variable which was incremented once. How to fix it?

like image 267
Илья Александрович Avatar asked May 19 '26 09:05

Илья Александрович


1 Answers

    AtomicInteger counter = new AtomicInteger(100);
    counter.addAndGet(2);
    System.out.println(counter);

or

    AtomicInteger counter = new AtomicInteger(100);        
    counter.incrementAndGet();
    counter.incrementAndGet();
    System.out.println(counter);
like image 51
Eritrean Avatar answered May 20 '26 23:05

Eritrean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!