# 十一种方法实现亿级数据++

(1)速度要快
(2)安全性要高

``````package com.example.currency03;

import java.util.concurrent.atomic.AtomicLong;

/**
*/
public class Counter1 {
public static AtomicLong inc = new AtomicLong();
public void increase() {
// 数据加一
inc.getAndIncrement();
}

public static void count() throws InterruptedException {
final Counter1 c1 = new Counter1();
long t1 = System.currentTimeMillis();

for(int i = 0; i < 1000; i++) {
th = new Thread(() -> {
for(int j = 0; j < 100000; j++) {
c1.increase();
}
});
th.start();
th.join();
}
long t2 = System.currentTimeMillis();
System.out.println("Counter1 , "+String.format("结果：%s,耗时(ms)：%s", inc, (t2 - t1)));
}

public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
inc.set(0);
count();
}
}
}
``````

``````package com.example.currency03;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
*/
public class Counter2 {
public static Integer inc = 0;
Lock lock = new ReentrantLock();
public void increase() {
lock.lock();
try {
inc++;
} finally {
lock.unlock();
}
}
public static void count() throws InterruptedException {
final Counter2 c2 = new Counter2();
Long t1 = System.currentTimeMillis();
for(int i = 0; i < 1000; i++) {
th = new Thread(() -> {
for(int j = 0; j < 100000; j++) {
c2.increase();
}
});
th.start();
th.join();
}
long t2 = System.currentTimeMillis();
System.out.println("Counter2 , "+String.format("结果：%s,耗时(ms)：%s", inc, (t2 - t1)));
}

public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
inc = 0;
count();
}
}
}
``````

``````package com.example.currency03;
/**
*/
public class Counter3 {
public static Integer inc = 0;
public synchronized void increase() {
inc++;
}
public static void count() throws InterruptedException {
final Counter3 c3 = new Counter3();
long t1 = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
th = new Thread(() -> {
for(int j = 0; j < 100000; j++) {
c3.increase();
}
});
th.start();
th.join();
}
long t2 = System.currentTimeMillis();
System.out.println("Counter3, "+String.format("结果：%s,耗时(ms)：%s", inc, (t2 - t1)));
}

public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
inc = 0;
count();
}

}
}
``````

``````package com.example.currency03;

import java.util.concurrent.atomic.AtomicLong;

/**
*/
public class Counter4 {
public static AtomicLong inc = new AtomicLong();
public void crease() {
inc.getAndIncrement();
}
public static void count() throws InterruptedException {
final Counter4 c4 = new Counter4();
long t1 = System.currentTimeMillis();
for(int i = 0; i < 1000; i++) {
th = new Thread(() -> {
for(int j = 0; j < 100000; j++) {
c4.crease();
}
});
th.start();
}
}
long t2 = System.currentTimeMillis();
System.out.println("Counter4, "+String.format("结果：%s,耗时(ms)：%s", inc, (t2 - t1)));
}

public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
inc.set(0);
count();
}
}

}
``````

``````package com.example.currency03;

import java.util.concurrent.atomic.AtomicLong;

/**
*/
public class Counter5 {
public static AtomicLong inc = new AtomicLong();
public void increase() {
inc.getAndIncrement();
}
public static void count() throws InterruptedException {
final Counter5 c5 = new Counter5();
long t1 = System.currentTimeMillis();
for(int i = 0; i < 1000; i++) {
th = new Thread(() -> {
for(int j = 0; j < 100000; j++) {
c5.increase();
}
});
th.start();
while(th.isAlive()){
}

}
long t2 = System.currentTimeMillis();
System.out.println("Counter5, "+String.format("结果：%s,耗时(ms)：%s", inc, (t2 - t1)));
}

public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
inc.set(0);
count();
}

}
}
``````

``````package com.example.currency03;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

/**
*/
public class Counter6 {
public static int inc = 0;
public synchronized static void increase() {
inc++;
}
public static void count() throws InterruptedException {
long t1 = System.currentTimeMillis();

for(int i = 0; i < threadCount; i++) {
try {
for (int j = 0; j < 100000; j++) {
increase();
}
} finally {
countDownLatch.countDown();
}

}).start();

}
countDownLatch.await();
long t2 = System.currentTimeMillis();
System.out.println("Counter6, "+String.format("结果：%s,耗时(ms)：%s", inc, (t2 - t1)));

}
public static void main(String[] args) throws ExecutionException, InterruptedException {
for (int i = 0; i < 10; i++) {
inc = 0;
count();
}
}

}
``````

``````package com.example.currency03;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;

/**
*/
public class Counter7 {
public static AtomicLong inc = new AtomicLong(0);
public static void increase() {
inc.getAndIncrement();
}
public static void count() throws InterruptedException {
long t1 = System.currentTimeMillis();
for(int i = 0; i < threadCount; i++) {
try {

for(int j = 0; j < 100000; j++) {
increase();
}

} finally {
countDownLatch.countDown();
}
}).start();
}
countDownLatch.await();
long t2 = System.currentTimeMillis();
System.out.println("Counter7, "+String.format("结果：%s,耗时(ms)：%s", inc, (t2 - t1)));
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
for (int i = 0; i < 10; i++) {
inc.set(0);
count();
}
}
}
``````

``````package com.example.currency03;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

/**
*/
public class Counter8 {

public static void increase() {
}

public static void count() throws InterruptedException {
long t1 = System.currentTimeMillis();

for (int i = 0; i < threadCount; i++) {
try {
for (int j = 0; j < 100000; j++) {
increase();
}

} finally {
countDownLatch.countDown();
}
}).start();
}
countDownLatch.await();
long t2 = System.currentTimeMillis();
System.out.println("Counter8, " + String.format("结果：%s,耗时(ms)：%s", longAdder.sum(), (t2 - t1)));

}

public static void main(String[] args) throws ExecutionException, InterruptedException {
for (int i = 0; i < 10; i++) {
count();
}
}
}``````

``````package com.example.currency03;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.LongAccumulator;

/**
*/
public class Counter9 {
private static LongAccumulator inc = new LongAccumulator((x, y) -> x + y, 0L);

public static void incr() {
inc.accumulate(1);
}

public static void count() throws InterruptedException {
long t1 = System.currentTimeMillis();
for(int i = 0; i < threadCount; i++) {
try {
for(int j = 0; j < 100000; j++) {
incr();
}
} finally {
countDownLatch.countDown();
}

}).start();
}
countDownLatch.await();
long t2 = System.currentTimeMillis();
System.out.println("Counter9, "+String.format("结果：%s,耗时(ms)：%s", inc.longValue(), (t2 - t1)));

}

public static void main(String[] args) throws ExecutionException, InterruptedException {
for (int i = 0; i < 10; i++) {
inc.reset();
count();
}
}

}
``````

``````package com.example.currency03;

/**
*/
public class Counter10 {

public  void increase() {
inc.increment();
}

public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
inc.reset();
count();
}

}

private static void count() throws InterruptedException {
final Counter10 test = new Counter10();
long t1 = System.currentTimeMillis();
for(int i=0;i<1000;i++){
for(int j = 0;j < 100000;j++) {
test.increase();
}
});
th.start();
th.join();
}

long t2 = System.currentTimeMillis();
System.out.println("Counter10 , "+String.format("结果：%s,耗时(ms)：%s", inc, (t2 - t1)));
}

}
``````

``````package com.example.currency03;

import java.util.concurrent.atomic.LongAccumulator;

/**
*/
public class Counter11 {
static LongAccumulator inc = new LongAccumulator((x, y) -> x + y, 0L);

public  void increase() {
inc.accumulate(1);
}

public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
inc.reset();
count();
}

}

private static void count() throws InterruptedException {
final Counter11 test = new Counter11();
long t1 = System.currentTimeMillis();
for(int i=0;i<1000;i++){
for(int j = 0; j < 100000; j++) {
test.increase();
}
});
th.start();
th.join();
}

long t2 = System.currentTimeMillis();
System.out.println("Counter11 , "+String.format("结果：%s,耗时(ms)：%s", inc, (t2 - t1)));
}

}
``````

GITHUB

12 声望
2 粉丝
0 条评论