MgcpTransactionNumberspace { public int generateId() { final int next = this.current.getAndIncrement(); boolean reset = this.current.compareAndSet(this.maximum + 1, this.minimum); if(reset) { if (log.isInfoEnabled()) { log.info("Reached maximum transaction identifier " + this.maximum + ". Reset number space to " + this.minimum); } } else { if (log.isTraceEnabled()) { log.trace("Current MGCP transaction identifier is " + next); } } return next; } MgcpTransactionNumberspace(int minimum, int maximum);  MgcpTransactionNumberspace(); int getMinimum(); int getMaximum(); int getCurrent(); int generateId();  }
@Test() public void testNoConcurrentCollisions() throws Exception { final int minimum = 1; final int maximum = 999; final MgcpTransactionNumberspace numberspace = new MgcpTransactionNumberspace(minimum, maximum); final NumberspaceConsumer consumer1 = new NumberspaceConsumer(numberspace, maximum / 3); final NumberspaceConsumer consumer2 = new NumberspaceConsumer(numberspace, maximum / 3); final NumberspaceConsumer consumer3 = new NumberspaceConsumer(numberspace, maximum / 3); new Thread(consumer1).start(); new Thread(consumer2).start(); new Thread(consumer3).start(); Thread.sleep(1000); final Set<Integer> uniques = new HashSet<>(maximum); assertFalse(existDuplicate(consumer1.numbers, uniques)); assertFalse(existDuplicate(consumer2.numbers, uniques)); assertFalse(existDuplicate(consumer3.numbers, uniques)); assertEquals(minimum, numberspace.generateId()); }