update at 2021-02-09 22:40:41 by ehlxr
This commit is contained in:
@@ -25,7 +25,10 @@
|
|||||||
package io.github.ehlxr.utils;
|
package io.github.ehlxr.utils;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
import java.util.concurrent.CyclicBarrier;
|
||||||
|
import java.util.concurrent.ForkJoinPool;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Twitter_Snowflake
|
* Twitter_Snowflake
|
||||||
@@ -224,17 +227,17 @@ public class SnowflakeIdUtil {
|
|||||||
// ExecutorService pool = Executors.newFixedThreadPool(count);
|
// ExecutorService pool = Executors.newFixedThreadPool(count);
|
||||||
ForkJoinPool pool = new ForkJoinPool(count);
|
ForkJoinPool pool = new ForkJoinPool(count);
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
pool.execute(() -> {
|
|
||||||
// 等待所有任务准备就绪
|
|
||||||
Try.of((Try.ThrowableRunnable) cyclicBarrier::await).trap(System.out::println).run();
|
|
||||||
|
|
||||||
long id = SnowflakeIdUtil.id();
|
IntStream.range(0, count).forEach(i ->
|
||||||
if (!ids.add(id)) {
|
pool.execute(() -> {
|
||||||
System.out.println(id);
|
// 等待所有任务准备就绪
|
||||||
}
|
Try.of((Try.ThrowableRunnable) cyclicBarrier::await).trap(System.out::println).run();
|
||||||
});
|
|
||||||
}
|
long id = SnowflakeIdUtil.id();
|
||||||
|
if (!ids.add(id)) {
|
||||||
|
System.out.println(id);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
pool.shutdown();
|
pool.shutdown();
|
||||||
while (!pool.isTerminated()) {
|
while (!pool.isTerminated()) {
|
||||||
|
Reference in New Issue
Block a user