void (boolean readLock, Consumer<? super Int2LongMap> consumer) { List<Exception> exceptions = new SmartList<>(); for (int i = 0; i < myLocks.length; i++) { Lock lock = null; try { ReadWriteLock readWriteLock = getLock(i); lock = readLock ? readWriteLock.readLock() : readWriteLock.writeLock(); lock.lock(); consumer.accept(getHashes(i)); } catch (Exception e) { exceptions.add(e); } finally { if (lock != null) { try { lock.unlock(); } catch (Exception e) { exceptions.add(e); } } } } if (!exceptions.isEmpty()) { IllegalStateException exception = new IllegalStateException("Exceptions while clearing"); for (Exception suppressed : exceptions) { exception.addSuppressed(suppressed); } throw exception; } }