File (File parent, String prefix, int max) { synchronized (ourDirectoryLock) { for (int i = 0; i < max; i++) { String name = prefix + i; File f = new File(parent, name); if (!f.exists()) { boolean createSuccessFull = f.mkdirs(); if (createSuccessFull) { return f; } } } throw new RuntimeException("No available dir found"); } }