AtomicInteger变量学习

This is an example of how to use the AtomicInteger class of Java. Thejava.util.concurrent.atomic package provides very useful classes that support lock-free and thread-safe programming on single variables. Among them, the AtomicInteger class is a wrapper class for an int value that allows it to be updated atomically. The class provides useful methods, some of which will be shown in the code snippet below.

The most common use of the AtomicInteger is to handle a counter that is accessed by different threads simultaneously. In order to see how this works, we will create and run two Threads, each one of which will access and update an AtomicInteger variable, using its API methods. The basic methods used in the example are described in short:

理解:AtomicInteger最常见的用法是作为多线程间的计数器.

  • With incrementAndGet() API method, the value is incremented and its new value is returned.
  • With getAndIncrement() API method, the value is incremented, but its previous value is returned.
  • With addAndGet(int delta) API method, the delta is added to the value and the new value is returned, whereas there is also a getAndAdd(int delta) method that adds the delta to the value, but returns the previous value.
  • With compareAndSet(int expect, int update) API method, the value is compared to the expect param, and if they are equal, then the value is set to the update param and true is returned.
  • You can get the intlongfloat or double value of the AtomicInteger variable, using intValue()longValue(),floatValue() and doubleValue() methods respectivelly.
package com.javacodegeeks.snippets.core;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerExample {

    private static AtomicInteger at = new AtomicInteger(0);

    static class MyRunnable implements Runnable {

        private int myCounter;
        private int myPrevCounter;
        private int myCounterPlusFive;
        private boolean isNine;

        public void run() {
            myCounter = at.incrementAndGet();
            System.out.println("Thread " + Thread.currentThread().getId() + "  / Counter : " + myCounter);
            myPrevCounter = at.getAndIncrement();
            System.out.println("Thread " + Thread.currentThread().getId() + " / Previous : " + myPrevCounter); 
            myCounterPlusFive = at.addAndGet(5);        
            System.out.println("Thread " + Thread.currentThread().getId() + " / plus five : " + myCounterPlusFive);
            isNine = at.compareAndSet(9, 3);
            if (isNine) {
                System.out.println("Thread " + Thread.currentThread().getId() 
                        + " / Value was equal to 9, so it was updated to " + at.intValue());
            }

        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable());
        Thread t2 = new Thread(new MyRunnable());
        t1.start();
        t2.start();
    }
}

If you run the example, you will see that both threads can update the AtomicInteger variable atomically.

Thread 9  / Counter : 1
Thread 10  / Counter : 2
Thread 9 / Previous : 2
Thread 9 / plus five : 9
Thread 9 / Value was equal to 9, so it was updated to 3
Thread 10 / Previous : 3
Thread 10 / plus five : 8

 

转载于:https://www.cnblogs.com/Guoyutian/p/5186882.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值