博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中的AtomicInteger
阅读量:2545 次
发布时间:2019-05-11

本文共 3987 字,大约阅读时间需要 13 分钟。

Today we will look into AtomicInteger in Java. Atomic operations are performed in a single unit of task without interference from other operations. Atomic operations are necessity in multi-threaded environment to avoid data inconsistency.

今天,我们将研究Java中的AtomicInteger原子操作在单个任务单元中执行,而不受其他操作的干扰。 在多线程环境中,原子操作是必需的,以避免数据不一致。

原子整数 (AtomicInteger)

Let’s create a simple multi-threaded program where every thread increments the shared count variable 4 times. So if there are two threads, after they finish count value should be 8.

让我们创建一个简单的多线程程序,其中每个线程将共享count变量递增4次。 因此,如果有两个线程,则在它们完成之后, count数值应为8。

JavaAtomic.java

JavaAtomic.java

package com.journaldev.concurrency;public class JavaAtomic {    public static void main(String[] args) throws InterruptedException {        ProcessingThread pt = new ProcessingThread();        Thread t1 = new Thread(pt, "t1");        t1.start();        Thread t2 = new Thread(pt, "t2");        t2.start();        t1.join();        t2.join();        System.out.println("Processing count=" + pt.getCount());    }}class ProcessingThread implements Runnable {    private int count;    @Override    public void run() {        for (int i = 1; i < 5; i++) {            processSomething(i);            count++;        }    }    public int getCount() {        return this.count;    }    private void processSomething(int i) {        // processing some job        try {            Thread.sleep(i * 1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

If you will run above program, you will notice that count value varies between 5,6,7,8. The reason is because count++ is not an atomic operation. So by the time one threads read it’s value and increment it by one, other thread has read the older value leading to wrong result.

如果您将在上述程序上运行,您会注意到count数值在5,6,7,8之间变化。 原因是因为count ++不是原子操作。 因此,当一个线程读取它的值并将其递增一个时,另一线程读取了较早的值,从而导致错误的结果。

To solve this issue, we will have to make sure that increment operation on count is atomic, we can do that using but Java 5 java.util.concurrent.atomic provides wrapper classes for int and long that can be used to achieve this atomic operation without usage of Synchronization.

为了解决这个问题,我们必须确保对count的增量操作是原子的,我们可以使用做到这一点,但是Java 5 java.util.concurrent.atomic为int和long提供了可用于实现此atomic的包装器类。不使用同步的情况下进行操作。

Java AtomicInteger示例 (Java AtomicInteger Example)

Here is the updated program that will always output count value as 8 because AtomicInteger method incrementAndGet() atomically increments the current value by one.

这是更新后的程序,该程序将始终将计数值输出为8,因为AtomicInteger方法incrementAndGet() AtomicInteger incrementAndGet()将当前值自动增加一。

package com.journaldev.concurrency;import java.util.concurrent.atomic.AtomicInteger;public class JavaAtomic {    public static void main(String[] args) throws InterruptedException {        ProcessingThread pt = new ProcessingThread();        Thread t1 = new Thread(pt, "t1");        t1.start();        Thread t2 = new Thread(pt, "t2");        t2.start();        t1.join();        t2.join();        System.out.println("Processing count=" + pt.getCount());    }}class ProcessingThread implements Runnable {    private AtomicInteger count = new AtomicInteger();    @Override    public void run() {        for (int i = 1; i < 5; i++) {            processSomething(i);            count.incrementAndGet();        }    }    public int getCount() {        return this.count.get();    }    private void processSomething(int i) {        // processing some job        try {            Thread.sleep(i * 1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

Benefits of using Concurrency classes for atomic operation is that we don’t need to worry about synchronization. This improves code readability and chance of errors are reduced. Also atomic operation concurrency classes are assumed to be more efficient that synchronization which involves locking resources.

使用并发类进行原子操作的好处是我们不必担心同步。 这样可以提高代码的可读性,并减少出错的机会。 同样,假定原子操作并发类比涉及锁定资源的同步更为有效。

翻译自:

转载地址:http://kqlzd.baihongyu.com/

你可能感兴趣的文章
搜索框键盘抬起事件2
查看>>
阿里百川SDK初始化失败 错误码是203
查看>>
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>
关于jquery中prev()和next()的用法
查看>>
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>
eclipse没有server选项
查看>>
CRC码计算及校验原理的最通俗诠释
查看>>
QTcpSocket的连续发送数据和连续接收数据
查看>>
使用Gitbook来编写你的Api文档
查看>>
jquery扩展 $.fn
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>