Java 提供的原子操作类
发布人:shili8
发布时间:2024-11-20 06:09
阅读次数:0
**Java 原子操作类**
在 Java 中,原子操作是指不会被中断或干扰的基本操作,如加法、减法等。这些操作对于多线程环境下的并发编程至关重要,因为它们可以保证数据的一致性和安全性。
Java 提供了一个名为 `java.util.concurrent.atomic` 的包,包含了一系列用于原子操作的类。这一包提供了各种基本类型(如 int、long 等)的原子引用,以及一些更复杂的原子操作类,如 `AtomicReferenceArray` 和 `AtomicLongArray`。
**基本原子操作类**
以下是 Java 中最常用的几个基本原子操作类:
###1. AtomicBoolean`AtomicBoolean` 类用于表示一个布尔值,并提供了原子的读写方法。它的使用场景包括多线程环境下的锁定和解锁。
javaimport java.util.concurrent.atomic.AtomicBoolean;
public class AtomicBooleanExample {
public static void main(String[] args) {
// 创建一个AtomicBoolean对象 AtomicBoolean lock = new AtomicBoolean(false);
// 线程1:尝试获取锁 Thread thread1 = new Thread(() -> {
if (lock.compareAndSet(false, true)) {
System.out.println("线程1获取了锁");
} else {
System.out.println("线程1未能获取锁");
}
});
// 线程2:尝试获取锁 Thread thread2 = new Thread(() -> {
if (lock.compareAndSet(false, true)) {
System.out.println("线程2获取了锁");
} else {
System.out.println("线程2未能获取锁");
}
});
// 启动两个线程 thread1.start();
thread2.start();
try {
// 等待两个线程执行完成 thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("锁的状态:" + lock.get());
}
}
###2. AtomicInteger`AtomicInteger` 类用于表示一个整数,并提供了原子的读写方法。它的使用场景包括多线程环境下的计数和累加。
javaimport java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerExample {
public static void main(String[] args) {
// 创建一个AtomicInteger对象 AtomicInteger count = new AtomicInteger(0);
// 线程1:增加计数 Thread thread1 = new Thread(() -> {
for (int i =0; i < 10000; i++) {
count.incrementAndGet();
}
});
// 线程2:增加计数 Thread thread2 = new Thread(() -> {
for (int i =0; i < 10000; i++) {
count.incrementAndGet();
}
});
// 启动两个线程 thread1.start();
thread2.start();
try {
// 等待两个线程执行完成 thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("计数:" + count.get());
}
}
###3. AtomicLong`AtomicLong` 类用于表示一个长整数,并提供了原子的读写方法。它的使用场景包括多线程环境下的计数和累加。
javaimport java.util.concurrent.atomic.AtomicLong;
public class AtomicLongExample {
public static void main(String[] args) {
// 创建一个AtomicLong对象 AtomicLong count = new AtomicLong(0);
// 线程1:增加计数 Thread thread1 = new Thread(() -> {
for (int i =0; i < 10000; i++) {
count.incrementAndGet();
}
});
// 线程2:增加计数 Thread thread2 = new Thread(() -> {
for (int i =0; i < 10000; i++) {
count.incrementAndGet();
}
});
// 启动两个线程 thread1.start();
thread2.start();
try {
// 等待两个线程执行完成 thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("计数:" + count.get());
}
}
###4. AtomicReference`AtomicReference` 类用于表示一个引用,并提供了原子的读写方法。它的使用场景包括多线程环境下的数据共享和传递。
javaimport java.util.concurrent.atomic.AtomicReference;
public class AtomicReferenceExample {
public static void main(String[] args) {
// 创建一个AtomicReference对象 AtomicReference data = new AtomicReference<>("Hello");
// 线程1:修改数据 Thread thread1 = new Thread(() -> {
String newData = "World";
if (data.compareAndSet("Hello", newData)) {
System.out.println("线程1修改了数据");
} else {
System.out.println("线程1未能修改数据");
}
});
// 线程2:读取数据 Thread thread2 = new Thread(() -> {
String dataValue = data.get();
System.out.println("线程2读取到的数据:" + dataValue);
});
// 启动两个线程 thread1.start();
thread2.start();
try {
// 等待两个线程执行完成 thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("最终数据:" + data.get());
}
}
###5. AtomicStampedReference`AtomicStampedReference` 类用于表示一个引用,并提供了原子的读写方法,包括时间戳。它的使用场景包括多线程环境下的数据共享和传递。
javaimport java.util.concurrent.atomic.AtomicStampedReference;
public class AtomicStampedReferenceExample {
public static void main(String[] args) {
// 创建一个AtomicStampedReference对象 AtomicStampedReference data = new AtomicStampedReference<>("Hello",0);
// 线程1:修改数据 Thread thread1 = new Thread(() -> {
String newData = "World";
int stamp = data.getStamp();
if (data.compareAndSet("Hello", newData, stamp +1)) {
System.out.println("线程1修改了数据");
} else {
System.out.println("线程1未能修改数据");
}
});
// 线程2:读取数据 Thread thread2 = new Thread(() -> {
String dataValue = data.getReference();
int stamp = data.getStamp();
System.out.println("线程2读取到的数据:" + dataValue + ", 时间戳:" + stamp);
});
// 启动两个线程 thread1.start();
thread2.start();
try {
// 等待两个线程执行完成 thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("最终数据:" + data.getReference() + ", 时间戳:" + data.getStamp());
}
}
###6. AtomicMarkableReference`AtomicMarkableReference` 类用于表示一个引用,并提供了原子的读写方法,包括标记。它的使用场景包括多线程环境下的数据共享和传递。
javaimport java.util.concurrent.atomic.AtomicMarkableReference;
public class AtomicMarkableReferenceExample {
public static void main(String[] args) {
// 创建一个AtomicMarkableReference对象 AtomicMarkableReference data = new AtomicMarkableReference<>("Hello", false);
// 线程1:修改数据 Thread thread1 = new Thread(() -> {
String newData = "World";
boolean marked = data.compareAndSet("Hello", newData, true);
System.out.println("线程1修改了数据:" + (marked ? "成功" : "失败"));
});
// 线程2:读取数据 Thread thread2 = new Thread(() -> {
String dataValue = data.getReference();
boolean marked = data.isMarked();
System.out.println("线程2读取到的数据:" + dataValue + ", 标记:" + (marked ? "已标记" : "未标记"));
});
// 启动两个线程 thread1.start();
thread2.start();
try {
// 等待两个线程执行完成 thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("最终数据:" + data.getReference() + ", 标记:" + (data.isMarked() ? "已标记" : "未标记"));
}
}
###7. AtomicLongArray`AtomicLongArray` 类用于表示一个长整数数组,并提供了原子的读写方法。它的使用场景包括多线程环境下的数据共享

