1.定义各类型自增方法
static class ClickNumber {
// synchronized方法
int number = 0;
public synchronized void add_synchronized() {
number++;
}
// atomicInteger
AtomicInteger atomicInteger = new AtomicInteger();
public void add_atomicInteger() {
atomicInteger.incrementAndGet();
}
// atomicLong
AtomicLong atomicLong = new AtomicLong();
public void add_atomicLong() {
atomicLong.incrementAndGet();
}
// longAccumulator
LongAccumulator longAccumulator = new LongAccumulator(Long::sum, 0);
public void add_longAccumulator() {
longAccumulator.accumulate(1);
}
// longAdder
LongAdder longAdder = new LongAdder();
public void add_longAdder() {
longAdder.increment();
}
}
2.编写测试方法
开启50个线程,每个线程执行100 * 10000次
/**
* 线程数量
*/
private static final int SIZE_THREAD = 50;
/**
* 循环次数
*/
private static final int COUNT = 10000;
@Test
public void test() throws InterruptedException {
ClickNumber clickNumber = new ClickNumber();
long startTime;
long endTime;
// 声明5个CountDownLatch对象,并且由主线程创建了50个线程,分别执行任务
CountDownLatch countDownLatch1 = new CountDownLatch(SIZE_THREAD);
CountDownLatch countDownLatch2 = new CountDownLatch(SIZE_THREAD);
CountDownLatch countDownLatch3 = new CountDownLatch(SIZE_THREAD);
CountDownLatch countDownLatch4 = new CountDownLatch(SIZE_THREAD);
CountDownLatch countDownLatch5 = new CountDownLatch(SIZE_THREAD);
/*****************************************************************************************************************/
startTime = System.currentTimeMillis();
for (int i = 1; i <= SIZE_THREAD; i++) {
new Thread(() -> {
try {
for (int j = 1; j <= 100 * COUNT; j++) {
clickNumber.add_synchronized();
}
} catch (Exception e) {
} finally {
countDownLatch1.countDown();
}
}, String.valueOf(i)).start();
}
// 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
countDownLatch1.await();
endTime = System.currentTimeMillis();
log.info("方法:add_synchronized --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.number);
/*****************************************************************************************************************/
startTime = System.currentTimeMillis();
for (int i = 1; i <= SIZE_THREAD; i++) {
new Thread(() -> {
try {
for (int j = 1; j <= 100 * COUNT; j++) {
clickNumber.add_atomicInteger();
}
} catch (Exception e) {
} finally {
countDownLatch2.countDown();
}
}, String.valueOf(i)).start();
}
// 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
countDownLatch2.await();
endTime = System.currentTimeMillis();
log.info("方法:add_atomicInteger --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.atomicInteger.get());
/*****************************************************************************************************************/
startTime = System.currentTimeMillis();
for (int i = 1; i <= SIZE_THREAD; i++) {
new Thread(() -> {
try {
for (int j = 1; j <= 100 * COUNT; j++) {
clickNumber.add_atomicLong();
}
} catch (Exception e) {
} finally {
countDownLatch3.countDown();
}
}, String.valueOf(i)).start();
}
// 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
countDownLatch3.await();
endTime = System.currentTimeMillis();
log.info("方法:add_atomicLong --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.atomicLong.get());
/*****************************************************************************************************************/
startTime = System.currentTimeMillis();
for (int i = 1; i <= SIZE_THREAD; i++) {
new Thread(() -> {
try {
for (int j = 1; j <= 100 * COUNT; j++) {
clickNumber.add_longAccumulator();
}
} catch (Exception e) {
} finally {
countDownLatch4.countDown();
}
}, String.valueOf(i)).start();
}
// 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
countDownLatch4.await();
endTime = System.currentTimeMillis();
log.info("方法:add_longAccumulator --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.longAccumulator.longValue());
/*****************************************************************************************************************/
startTime = System.currentTimeMillis();
for (int i = 1; i <= SIZE_THREAD; i++) {
new Thread(() -> {
try {
for (int j = 1; j <= 100 * COUNT; j++) {
clickNumber.add_longAdder();
}
} catch (Exception e) {
} finally {
countDownLatch5.countDown();
}
}, String.valueOf(i)).start();
}
// 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
countDownLatch5.await();
endTime = System.currentTimeMillis();
log.info("方法:add_longAdder --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.longAdder.longValue());
}
3.对比测试结果
由此可见,LongAdder牛逼。