private String lockRandomNamespace(final boolean readOnly){
  final int k=r.nextInt((int)namespaceExistCounter.get());
  int i=-1;
  while (true) {
    for (    Map.Entry<String,ReadWriteLock> e : namespaces.entrySet()) {
      if (namespaceExistCounter.get() == 0) {
        throw new RuntimeException("No namespaces? readOnly=" + readOnly);
      }
      i++;
      if (i < k) {
        continue;
      }
      final String namespace=e.getKey();
      final ReadWriteLock lock=e.getValue();
      final Lock takenLock;
{
        if (readOnly)         takenLock=lock.readLock();
 else         takenLock=lock.writeLock();
        takenLock.lock();
      }
      if (namespaces.get(namespace) != lock) {
        takenLock.unlock();
        continue;
      }
      return namespace;
    }
  }
}

private String lockRandomNamespace ( final boolean readOnly ) { final int k = r . nextInt ( ( int ) namespaceExistCounter . get ( ) ) ; int i = - 1 ; while ( true ) { for ( Map . Entry < String , ReadWriteLock > e : namespaces . entrySet ( ) ) { if ( namespaceExistCounter . get ( ) == 0 ) { throw new RuntimeException ( "No namespaces? readOnly=" + readOnly ) ; } i ++ ; if ( i < k ) { continue ; } final String namespace = e . getKey ( ) ; final ReadWriteLock lock = e . getValue ( ) ; final Lock takenLock ; { if ( readOnly ) takenLock = lock . readLock ( ) ; else takenLock = lock . writeLock ( ) ; takenLock . lock ( ) ; } if ( namespaces . get ( namespace ) != lock ) { takenLock . unlock ( ) ; continue ; } return namespace ; } } }
return a namespace at random from the set of known to exist namespaces . the caller will hold the appropriate lock .
