【发布时间】:2020-06-24 05:22:27
【问题描述】:
我正在测试HashIds 的碰撞。这是依赖关系:
<!-- https://mvnrepository.com/artifact/org.hashids/hashids -->
<dependency>
<groupId>org.hashids</groupId>
<artifactId>hashids</artifactId>
<version>1.0.3</version>
</dependency>
我正在使用以下代码:
Hashids hashids = new Hashids("xyz", 6, "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ");
System.out.println("*******************************************************************");
System.out.println("Started");
Set<String> set = new HashSet();
ExecutorService executor = Executors.newFixedThreadPool(4);
AtomicLong count = new AtomicLong(0);
final long max = 10_000_000;
for (long i = 1; i <= max; i++) {
executor.execute(() -> {
set.add(hashids.encode(count.incrementAndGet()));
// This is just to show me that there is some activity going on
if (count.get() % 100_000 == 0) {
System.out.println(count.get() + ": " + new Date());
}
});
}
// Wait till the executor service tasks are done
executor.shutdown();
while (!executor.isTerminated()) {
Thread.sleep(1000);
}
// Assert that the Set did not receive duplicates
Assert.assertEquals(max, set.size());
System.out.println("Ended");
System.out.println("*******************************************************************");
所以,我放了一个ExecutorService 让它更快一点,但我遇到了问题。要么
-
ExecutorService未完成并挂在那里 -
Set包含重复值,因此断言失败 - 使用裸
for循环比使用ExecutorService快得多
这段代码有什么问题?
【问题讨论】:
标签: java multithreading