【Java】线程安全之原子性Atomic(AtomicInteger|LongAdder|AtomicLong)

线程安全性

原子性

Atomic包

案例

package com.keytech.task;

import java.util.concurrent.Executor;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

import java.util.concurrent.atomic.AtomicInteger;

import java.util.concurrent.atomic.LongAdder;

/**

* @className: AtomicTest

* @description: TODO 类描述

* @author: mac

* @date: 2020/12/27

**/

public class AtomicTest {

private static Integer clientTotal=5000;

private static Integer threadTotal=200;

private static AtomicInteger count=new AtomicInteger(0);

public static void main(String[] args) {

ExecutorService executorService = Executors.newCachedThreadPool();

Semaphore semaphore= new Semaphore(threadTotal);

for (int i = 0; i <clientTotal ; i++) {

executorService.execute(()->{

try{

semaphore.acquire();

update();

semaphore.release();

}catch (Exception e){

e.printStackTrace();

}

});

}

executorService.shutdown();

System.out.println("count:"+count);

}

private static void update(){

count.incrementAndGet();

}

}

getAndAddInt源码

public final int getAndAddInt(Object var1, long var2, int var4) {

int var5;

do {

var5 = this.getIntVolatile(var1, var2);

} while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));

return var5;

}

LongAddr

package com.keytech.task;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

import java.util.concurrent.atomic.LongAdder;

/**

* @className: LongAddrTest

* @description: TODO 类描述

* @author: mac

* @date: 2020/12/28

**/

public class LongAddrTest {

private static Integer clientTotal=5000;

private static Integer threadTotal=200;

private static LongAdder count=new LongAdder();

public static void main(String[] args) {

ExecutorService executorService = Executors.newCachedThreadPool();

Semaphore semaphore=new Semaphore(threadTotal);

for (int i = 0; i < clientTotal; i++) {

try{

semaphore.acquire();

update();

semaphore.release();

}catch (Exception e){

e.printStackTrace();

}

}

executorService.shutdown();

System.out.println("count"+count);

}

private static void update(){

count.increment();

}

}

AtomicLong

package com.keytech.task;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

import java.util.concurrent.atomic.AtomicLong;

/**

* @className: AtomicLongTest

* @description: TODO 类描述

* @author: mac

* @date: 2020/12/28

**/

public class AtomicLongTest {

private static Integer clientTotal=5000;

private static Integer threadTotal=200;

private static AtomicLong count=new AtomicLong();

public static void main(String[] args) {

ExecutorService executorService = Executors.newCachedThreadPool();

Semaphore semaphore=new Semaphore(threadTotal);

for (int i = 0; i < clientTotal; i++) {

try{

semaphore.tryAcquire();

update();

semaphore.release();

}catch (Exception e){

e.printStackTrace();

}

}

executorService.shutdown();

System.out.println("count"+count);

}

private static void update(){

count.incrementAndGet();

}

}

LongAddr与AtomicLong的区别

【Java】线程安全之原子性Atomic(AtomicInteger|LongAdder|AtomicLong)

LongAddr与AtomicLong的使用场景

【Java】线程安全之原子性Atomic(AtomicInteger|LongAdder|AtomicLong)

以上是 【Java】线程安全之原子性Atomic(AtomicInteger|LongAdder|AtomicLong) 的全部内容, 来源链接: utcz.com/a/89649.html

回到顶部