private void dispatchCalls()

in sdk/core/azure-core-http/src/main/java/com/azure/android/core/http/HttpCallDispatcher.java [260:301]


    private void dispatchCalls() {
        Deque<DispatchableCall> executableCalls = new ArrayDeque<>();
        synchronized (this) {
            // Collects the calls to dispatch.

            // 1. Collects the executable NestedDispatchableCall calls.
            //    Note: Collecting NestedDispatchableCall calls first to have them in front of executable queue.
            while (this.runningRootDispatchableCalls.size() < this.maxRunningCalls
                && !this.waitingNestedDispatchableCalls.isEmpty()) {
                final NestedDispatchableCall nestedCall = this.waitingNestedDispatchableCalls.poll();
                this.runningRootDispatchableCalls.add(nestedCall.rootDispatchableCall);
                executableCalls.add(nestedCall);
            }

            // 2. Collects the executable RootDispatchableCall calls.
            while (this.runningRootDispatchableCalls.size() < this.maxRunningCalls
                && !this.waitingRootDispatchableCalls.isEmpty()) {
                final RootDispatchableCall rootCall = this.waitingRootDispatchableCalls.poll();
                this.runningRootDispatchableCalls.add(rootCall);
                executableCalls.add(rootCall);
            }
        }

        // Dispatch the collected calls on dispatcher threads.
        // Dispatching must be done outside sync-block since calling into user-code while holding
        // lock is prohibited.
        while (!executableCalls.isEmpty()) {
            final DispatchableCall call = executableCalls.poll();
            assert call != null;
            try {
                this.executorService.execute(call);
            } catch (RejectedExecutionException e) {
                call.onError(new InterruptedIOException("executor rejected").initCause(e));
            } catch (Throwable t) {
                // The ExecutorService::execute() is not supposed to throw any exception other than
                // RejectedExecutionException, but if it ever throws other exceptions, let's do the
                // cleanup and then rethrow.
                call.markNotRunning(1);
                throw logger.logExceptionAsError(new RuntimeException("ExecutorService::schedule failed.", t));
            }
        }
    }