我的项目需要:实现多线程达到数据的亿级自增,并且尽可能让工夫缩短 , 要求:
(1) 速度要快
(2) 安全性要高
第 1 种形式
package com.example.currency03;
import java.util.concurrent.atomic.AtomicLong;
/**
* 1· 使用 Thread、AtomicLong、线程 join 实现
*/
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();
Thread th;
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();}
}
}
消耗工夫如下:
第 2 种形式
package com.example.currency03;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 2、使用 thread、reentrantlock、线程 join
*/
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();
Thread th;
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();}
}
}
消耗工夫如下:
第 3 种形式
package com.example.currency03;
/**
* 3、使用 thread、synchronized、线程 join
*/
public class Counter3 {
public static Integer inc = 0;
public synchronized void increase() {inc++;}
public static void count() throws InterruptedException {final Counter3 c3 = new Counter3();
Thread th;
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();}
}
}
消耗工夫如下:
第 4 种形式
package com.example.currency03;
import java.util.concurrent.atomic.AtomicLong;
/**
* 4、使用 thread、AtomicLong、线程状态 TERMINATED
*/
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();
Thread th = null;
for(int i = 0; i < 1000; i++) {th = new Thread(() -> {for(int j = 0; j < 100000; j++) {c4.crease();
}
});
th.start();
while(th.getState()!=Thread.State.TERMINATED){Thread.sleep(1);
}
}
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();}
}
}
消耗工夫如下:
第 5 种形式
package com.example.currency03;
import java.util.concurrent.atomic.AtomicLong;
/**
* 5、使用 Thread、AtomicLong、线程 isAlive 等实现
*/
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();
Thread th = null;
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()){Thread.sleep(1);
}
}
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();}
}
}
消耗工夫如下:
第 6 种形式
package com.example.currency03;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
/**
* 6、使用 Thread、synchronized、线程 CountDownLatch 等实现
*/
public class Counter6 {
public static int inc = 0;
public synchronized static void increase() {inc++;}
public static void count() throws InterruptedException {
int threadCount = 1000;
long t1 = System.currentTimeMillis();
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for(int i = 0; i < threadCount; i++) {new Thread(() -> {
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();}
}
}
消耗工夫如下:
第 7 种形式
package com.example.currency03;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;
/**
* 7、使用 Thread、AtomicLong、线程 CountDownLatch 等实现
*/
public class Counter7 {public static AtomicLong inc = new AtomicLong(0);
public static void increase() {inc.getAndIncrement();
}
public static void count() throws InterruptedException {
int threadCount = 1000;
long t1 = System.currentTimeMillis();
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for(int i = 0; i < threadCount; i++) {new Thread(() -> {
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();}
}
}
消耗工夫如下:
第 8 种形式
package com.example.currency03;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.LongAdder;
/**
* 8、使用 Thread、LongAddr、线程 CountDownLatch 等实现
*/
public class Counter8 {public static LongAdder longAdder = new LongAdder();
public static void increase() {longAdder.increment();
}
public static void count() throws InterruptedException {
int threadCount = 1000;
long t1 = System.currentTimeMillis();
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {new Thread(() -> {
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++) {longAdder.reset();
count();}
}
}
消耗工夫如下:
第 9 种形式
package com.example.currency03;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.LongAccumulator;
/**
* 9、使用 Thread、LongAccumulator、线程 CountDownLatch 等实现
*/
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 {
int threadCount = 1000;
long t1 = System.currentTimeMillis();
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for(int i = 0; i < threadCount; i++) {new Thread(() -> {
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();}
}
}
消耗工夫如下:
第 10 种形式
package com.example.currency03;
import java.util.concurrent.atomic.LongAdder;
/**
* 10、使用 Thread、LongAdder、线程 join 的实现
*/
public class Counter10 {public static LongAdder inc = new LongAdder();
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();
Thread th;
long t1 = System.currentTimeMillis();
for(int i=0;i<1000;i++){th= new Thread(() -> {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)));
}
}
消耗工夫如下:
第 11 种形式
package com.example.currency03;
import java.util.concurrent.atomic.LongAccumulator;
/**
* 使用 Thread、LongAccumulator、线程 join 的实现
*/
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();
Thread th;
long t1 = System.currentTimeMillis();
for(int i=0;i<1000;i++){th= new Thread(() -> {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