高并发编程之AtomicLong讲解
转:https://blog.csdn.net/weixin_42146366/article/details/87820373
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//1、创建具有初始值 0 的新 AtomicLong。
AtomicLong atomicLong = new AtomicLong();
System.out.println(“Value:” + atomicLong.get());
}
}
②.创建具有给定初始值的新 AtomicLong。
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//2、创建具有给定初始值的新 AtomicLong。
AtomicLong atomicLong1 = new AtomicLong(10);
System.out.println(“Value:” + atomicLong1.get());
}
}
③.addAndGet()方法:以原子方式将给定值添加到当前值,先加上特定的值,再获取结果
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//3、以原子方式将给定值添加到当前值,先加上特定的值,再获取结果
AtomicLong atomicLong2 = new AtomicLong(3);
atomicLong2.addAndGet(5);
System.out.println(“Value:” + atomicLong2.get());
}
}
④.getAndAdd()方法:先获取当前值再加上特定的值
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//4、先获取当前值再加上特定的值
AtomicLong atomicLong5 = new AtomicLong(10);
atomicLong5.getAndAdd(5);
System.out.println(“Value:” + atomicLong5.get());
}
}
⑤.compareAndSet()方法:如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//5、如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
AtomicLong atomicLong3 = new AtomicLong(10);
atomicLong3.compareAndSet(10,15);
System.out.println(“Value:” + atomicLong3.get());
}
}
⑥.decrementAndGet()方法:以原子方式将当前值减 1,先减去1再获取值
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//6、 以原子方式将当前值减 1,先减去1再获取值
AtomicLong atomicLong4 = new AtomicLong(10);
atomicLong4.decrementAndGet();
System.out.println(“Value:” + atomicLong4.get());
}
}
⑦.getAndDecrement()方法:先获取当前值再减1
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//7、先获取当前值再减1
AtomicLong atomicLong6 = new AtomicLong();
atomicLong6.getAndDecrement();
System.out.println(“Value:” + atomicLong6.get());
}
}
⑧.getAndIncrement()方法:先获取当前值再加1
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//8、先获取当前值再加1
AtomicLong atomicLong7 = new AtomicLong();
atomicLong7.getAndIncrement();
System.out.println(“Value:” + atomicLong7.get());
}
}
⑨.incrementAndGet()方法:先加1再获取当前值
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//9、先加1再获取当前值
AtomicLong atomicLong9 = new AtomicLong();
atomicLong9.incrementAndGet();
System.out.println(“Value:” + atomicLong9.get());
}
}
⑩.getAndSet()方法:先获取当前值再设置新的值
import java.util.concurrent.atomic.AtomicLong;
/**
- @author czd
*/
public class AtomicLongTest {
public static void main(String[] args) {
//10、先获取当前值再设置新的值
AtomicLong atomicLong8 = new AtomicLong();
atomicLong8.getAndSet(20);
System.out.println(“Value:” + atomicLong8.get());
}
}
相关文章
- Go 并发编程与定时器
- Java 并发编程中的死锁 ( Kotlin 语言讲解)
- Juc并发编程12——2万字深入源码:线程池这篇真的讲解的透透的了
- 精讲高并发核心编程,限流原理与实战,限流策略原理与参考实现
- lofter限流怎么解决_高并发限流
- Go(七)你说你不会并发?
- 【深入浅出Java原理及实战】「并发编程系列」深入分析线程池的工作原理(上篇)
- 并发与竞争
- 并发编程Bug起源:可见性、有序性和原子性问题
- 【浅尝高并发编程】接私活差点翻车
- python-高级协程编程-协程的并发控制(一)
- Java 多线程并发编程之 Synchronized 关键字详解编程语言
- Redis负载均衡:优化高并发环境下的分布式系统配置(redis负载均衡配置)
- Redis乐观锁:高效实现数据并发控制(redis乐观锁)
- 实现高效并发编程:Linux系统的必备技能(linux实现并发)
- Linux并发编程实现:提高程序运行效率的必要技能(linux并发实现)
- 存储海量数据,Redis存储更便捷(并发量多大用redis)
- 在并发环境中利用Redis锁机制实现安全访问(并发下+redis锁机制)
- java线程并发semaphore类示例