/**
  * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately
  * cancelled.
  */
 @Override
 public ManagedChannelImpl shutdown() {
   ArrayList<TransportSet> transportsCopy = new ArrayList<TransportSet>();
   synchronized (lock) {
     if (shutdown) {
       return this;
     }
     shutdown = true;
     // After shutdown there are no new calls, so no new cancellation tasks are needed
     scheduledExecutor = SharedResourceHolder.release(TIMER_SERVICE, scheduledExecutor);
     if (transports.isEmpty()) {
       terminated = true;
       lock.notifyAll();
       onChannelTerminated();
     } else {
       transportsCopy.addAll(transports.values());
     }
   }
   loadBalancer.shutdown();
   nameResolver.shutdown();
   for (TransportSet ts : transportsCopy) {
     ts.shutdown();
   }
   return this;
 }
  ManagedChannelImpl(
      String target,
      BackoffPolicy.Provider backoffPolicyProvider,
      NameResolver.Factory nameResolverFactory,
      Attributes nameResolverParams,
      LoadBalancer.Factory loadBalancerFactory,
      ClientTransportFactory transportFactory,
      DecompressorRegistry decompressorRegistry,
      CompressorRegistry compressorRegistry,
      @Nullable Executor executor,
      @Nullable String userAgent,
      List<ClientInterceptor> interceptors) {
    if (executor == null) {
      usingSharedExecutor = true;
      this.executor = SharedResourceHolder.get(GrpcUtil.SHARED_CHANNEL_EXECUTOR);
    } else {
      usingSharedExecutor = false;
      this.executor = executor;
    }
    this.backoffPolicyProvider = backoffPolicyProvider;
    this.nameResolver = getNameResolver(target, nameResolverFactory, nameResolverParams);
    this.loadBalancer = loadBalancerFactory.newLoadBalancer(nameResolver.getServiceAuthority(), tm);
    this.transportFactory = transportFactory;
    this.userAgent = userAgent;
    this.interceptorChannel = ClientInterceptors.intercept(new RealChannel(), interceptors);
    scheduledExecutor = SharedResourceHolder.get(TIMER_SERVICE);
    this.decompressorRegistry = decompressorRegistry;
    this.compressorRegistry = compressorRegistry;

    this.nameResolver.start(
        new NameResolver.Listener() {
          @Override
          public void onUpdate(List<ResolvedServerInfo> servers, Attributes config) {
            loadBalancer.handleResolvedAddresses(servers, config);
          }

          @Override
          public void onError(Status error) {
            Preconditions.checkArgument(!error.isOk(), "the error status must not be OK");
            loadBalancer.handleNameResolutionError(error);
          }
        });
  }