public static void main(String[] args) throws InterruptedException { int count=2000; CountDownLatch cdl=new CountDownLatch(count); IdGeneratorService service = new IdGeneratorService(); Map<Long, AtomicLong> countMap=new ConcurrentHashMap<>(); for(long i=1;i<=count;i++){ countMap.put(i,new AtomicLong()); } for(int i=0;i<count;i++){ new Thread(()->{ long id = service.nextId("test"); countMap.get(id).incrementAndGet(); cdl.countDown(); }).start(); } cdl.await(); boolean match = countMap.values().stream().mapToLong(AtomicLong::get).anyMatch(l->l>1); System.out.printf("id 重复=%b\n",match); } 