LongAdder优于AtomicLong
public class TestAtomicLong {
private static AtomicLong ai = new AtomicLong(0);
public static void main(String[] args) throws InterruptedException {
MyThread mt = new MyThread();
Long time = System.currentTimeMillis();
System.out.println("当前时间:" + time);
for (int i = 0; i < 4; i++) {
Thread t = new Thread(mt);
t.start();
}
Thread.sleep(5000);
}
static class MyThread implements Runnable {
@Override
public void run() {
while (true) {
if (ai.get() >= 100000000L) {
System.out.println("已达到1亿");
System.out.println(System.currentTimeMillis());
return;
}
ai.getAndIncrement();
}
}
}
}
public class TestLongAdder {
private static LongAdder ai = new LongAdder();
public static void main(String[] args) throws InterruptedException {
MyThread mt = new MyThread();
Long time = System.currentTimeMillis();
System.out.println("当前时间:" + time);
for (int i = 0; i < 4; i++) {
Thread t = new Thread(mt);
t.start();
}
Thread.sleep(5000);
}
static class MyThread implements Runnable {
@Override
public void run() {
while (true) {
if (ai.longValue() >= 100000000L) {
System.out.println("已达到1亿");
System.out.println(System.currentTimeMillis());
return;
}
ai.increment();
}
}
}
}
标签:自增,ai,System,public,static,new,线程,java8,out
来源: https://blog.csdn.net/link88889/article/details/110123117