TransactionServiceImpl implements TransactionService { public String getNextTransactionId() { return String.valueOf(uniqueIdGen.incrementAndGet()); } @Inject  TransactionServiceImpl(TransactionInMemoryCache memCache,
                                TransactionFactory factory); String getNextTransactionId(); void commit(String txId); void rollback(String txId); void save(TransactionElement<T> element); void update(TransactionElement<T> element); void delete(TransactionElement<T> element);  }
@Test public void testConcurrentIdGeneration() { final TransactionService service = injector.getInstance(TransactionService.class); final Set<String> txIds = new ConcurrentSkipListSet<String>(); final int threadCount = 10; final int loopCount = 100000; List<Thread> threads = new ArrayList<Thread>(); long start = System.currentTimeMillis(); for (int i = 0; i < threadCount; ++i) { Thread thread = new Thread(new Runnable() { public void run() { for (int j = 0; j < loopCount; ++j) { txIds.add(service.getNextTransactionId()); } } }); thread.start(); threads.add(thread); } for (Thread thread : threads) { try { thread.join(); } catch (Exception ex) { LOGGER.error("Error waiting for a thread", ex); } } long end = System.currentTimeMillis(); Assert.assertEquals((threadCount * loopCount), txIds.size()); System.out.println("Time taken to generate " + (threadCount * loopCount) + " ids is " + (end - start)); }