DefaultOrderNumberGeneratorImpl implements OrderNumberGenerator { void setLastCheck(final LocalDateTime lastCheck) { this.lastCheck = lastCheck; } DefaultOrderNumberGeneratorImpl(final GenericDAO<CustomerOrder, Long> customerOrderDao);   DefaultOrderNumberGeneratorImpl(); @Override synchronized String getNextOrderNumber();  }
@Test public void testGetNextOrderNumberMultithread() throws Exception { final LocalDateTime lastCheck = DateUtils.ldtParseSDT("2017-01-01 00:00:00"); int THREADGROUPSIZE = 100; DefaultOrderNumberGeneratorImpl defaultOrderNumberGenerator = new DefaultOrderNumberGeneratorImpl() { @Override LocalDateTime now() { return DateUtils.ldtParseSDT("2017-01-01 11:11:11"); } }; defaultOrderNumberGenerator.setLastCheck(lastCheck); Set<String> rez = Collections.synchronizedSet(new HashSet<String>()); final CyclicBarrier gate = new CyclicBarrier(THREADGROUPSIZE); Thread[] th = new Thread[THREADGROUPSIZE]; for (int i = 0; i < THREADGROUPSIZE; i++) { th[i] = new MyOrderCreatorThread(defaultOrderNumberGenerator, rez, gate); th[i].start(); } for (int i = 0; i < THREADGROUPSIZE; i++) { try { th[i].join(); } catch (InterruptedException e) { } } assertEquals(THREADGROUPSIZE, rez.size()); }