Java 中的原子变量示例

原文:https://www . geesforgeks . org/atomic-variables-in-Java-with-examples/

多线程 中,共享实体在合并并发时大多会导致问题。共享实体,例如,可变的对象或变量,可能会被更改,这可能会导致程序或数据库的不一致。因此,处理并发访问的共享实体变得至关重要。在这种情况下,原子变量可以是备选方案之一。

Java 提供原子类,如 AtomicIntegerAtomicLongAtomicBooleanAtomicReference 。这些类的对象分别表示 int、long、boolean 和 object reference 的原子变量。这些类包含以下方法。

  1. *设置(int 值):*设置给定值
  2. *get():* 获取当前值
  3. *lazySet(int 值):*最终设置为给定值
  4. *比较数据集(int expect,int update):* 如果当前值==期望值,则自动将该值设置为给定的更新值
  5. *addAndGet(int delta):* 自动将给定值加到当前值上
  6. *递减和获取():*自动将当前值递减 1

*示例:*

// Atomic Variable
AtomicInteger var;

*需要原子变量* 考虑下面的例子:

Java 语言(一种计算机语言,尤用于创建网站)

class Counter extends Thread {

    // Counter Variable
    int count = 0;

    // method which would be called upon
    // the start of execution of a thread
    public void run()
    {
        int max = 1_000_00_000;

        // incrementing counter
        // total of max times
        for (int i = 0; i < max; i++) {
            count++;
        }
    }
}

public class UnSafeCounter {
    public static void main(String[] args)
        throws InterruptedException
    {
        // Instance of Counter Class
        Counter c = new Counter();

        // Defining Two different threads
        Thread first = new Thread(c, "First");
        Thread second = new Thread(c, "Second");

        // Threads start executing
        first.start();
        second.start();

        // main thread will wait for
        // both threads to get completed
        first.join();
        second.join();

        // Printing final value of count variable
        System.out.println(c.count);
    }
}

**Output: 

137754082
```** 

**在单线程环境中上述类只会给出预期的结果但是当涉及多线程环境时可能会导致不一致的结果发生这种情况是因为更新var分三步完成:读取更新和写入如果两个或多个线程试图同时更新该值则它可能无法正确更新**

**使用[锁定和同步](https://www.geeksforgeeks.org/lock-framework-vs-thread-synchronization-in-java/)可以解决这个问题,但是效率不高。**

****1使用锁类比或同步:**同步或锁定可以解决我们的问题但它会损害时间效率或性能首先它要求资源和线程调度器控制锁第二当多个线程试图获取锁时只有一个线程获胜其余的线程被挂起或阻塞挂起或阻塞线程会对性能产生巨大影响**

## **Java 语言(一种计算机语言尤用于创建网站)**

```java
import java.io.*;
import java.util.concurrent.locks.*;

class Counter extends Thread {

    // Counter Variable
    int count = 0;

    // method which would be called upon
    // the start of execution of a thread
    public synchronized void run()
    {

        int max = 1_000_00_000;

        // incrementing counter total of max times
        for (int i = 0; i < max; i++) {
            count++;
        }
    }
}

public class SynchronizedCounter {
    public static void main(String[] args)
        throws InterruptedException
    {
        // Instance of Counter Class
        Counter c = new Counter();

        // Defining Two different threads
        Thread first = new Thread(c, "First");
        Thread second = new Thread(c, "Second");

        // Threads start executing
        first.start();
        second.start();

        // main thread will wait for both
        // threads to complete execution
        first.join();
        second.join();

        // Printing final value of count variable
        System.out.println(c.count);
    }
}

**Output: 

200000000
```** 

****2使用原子变量:****

## **Java 语言(一种计算机语言尤用于创建网站)**

```java
import java.util.concurrent.atomic.AtomicInteger;

class Counter extends Thread {

    // Atomic counter Variable
    AtomicInteger count;

    // Constructor of class
    Counter()
    {
        count = new AtomicInteger();
    }

    // method which would be called upon
    // the start of execution of a thread
    public void run()
    {

        int max = 1_000_00_000;

        // incrementing counter total of max times
        for (int i = 0; i < max; i++) {
            count.addAndGet(1);
        }
    }
}

public class AtomicCounter {
    public static void main(String[] args)
        throws InterruptedException
    {
        // Instance of Counter Class
        Counter c = new Counter();

        // Defining Two different threads
        Thread first = new Thread(c, "First");
        Thread second = new Thread(c, "Second");

        // Threads start executing
        first.start();
        second.start();

        // main thread will wait for both
        // threads to complete execution
        first.join();
        second.join();

        // Printing final value of count variable
        System.out.println(c.count);
    }
}

**Output: 

java 200000000**