Hadoop源码分析9:IPC流程(4) Client 的 wait() 和 notify()

1.  ClientCall的 wait() 和 notify()

public class Client {

    public Writable call(Writable param, ClientConnectionId remoteId) 
                        throwsInterruptedException, IOException {
    ClientCall call =new ClientCall(param,this);
    ClientConnectionconnection = getConnection(remoteId, call);
    connection.sendParam(call);                 // sendthe parameter
    boolean interrupted =false;
    synchronized(call) {
      while(!call.done) {
        try {
          call.wait()                         // wait for the result
        } catch (InterruptedException ie) {
          // save the fact that we wereinterrupted
          interrupted = true;
        }
      }
      if(interrupted) {
        // set the interrupt flag now that we are donewaiting
        Thread.currentThread().interrupt();
      }
      if(call.error != null) {
        if (call.error instanceof RemoteException){
          call.error.fillInStackTrace();
          throw call.error;
        } else { // local exception
          // use the connection becauseit will reflect an ip change, unlike
          // the remoteId
          throwwrapException(connection.getRemoteAddress(), call.error);
        }
      }else {
        return call.value;
      }
    }
    }
  }



public class ClientCall {

     
    protected synchronized void callComplete() {
      this.done = true;
      notify();                                 // notify caller
    }
}

2.   ClientConnection 的 wait()和 notify()


public class ClientConnection extends Thread {

    private synchronized boolean waitForWork() {
      if(calls.isEmpty() && !shouldCloseConnection.get()  && client.running.get())  {
        long timeout = maxIdleTime-
              (System.currentTimeMillis()-lastActivity.get());
        if (timeout>0) {
          try {
            wait(timeout);
          } catch (InterruptedExceptione) {}
        }
      }
       
      if(!calls.isEmpty() && !shouldCloseConnection.get()&& client.running.get()) {
        return true;
      }else if (shouldCloseConnection.get()) {
        return false;
      }else if (calls.isEmpty()) { // idle connection closed orstopped
        markClosed(null);
        return false;
      }else { // get stopped but there are still pendingrequests 
        markClosed((IOException)newIOException().initCause(
            newInterruptedException()));
        return false;
      }
    }

    public synchronized boolean addCall(ClientCall call) {
      if(shouldCloseConnection.get())
        return false;
      calls.put(call.id, call);
      notify();
      return true;
    }

     
    private synchronized void markClosed(IOException e) {
      if(shouldCloseConnection.compareAndSet(false, true)) {
        closeException = e;
        notifyAll();
      }
   
}

3.ClientParallelResults的wait() 和 notify()

public class Client {

    public Writable[] call(Writable[] params, InetSocketAddress[] addresses,
      Class<?> protocol, UserGroupInformation ticket, Configurationconf)
      throws IOException, InterruptedException {
    if (addresses.length ==0) return new Writable[0];

    ClientParallelResultsresults = new ClientParallelResults(params.length);
    synchronized (results){
      for(int i = 0; i < params.length; i++) {
        ClientParallelCall call = newClientParallelCall(params[i], results, i,this);
        try {
          ClientConnectionId remoteId =ClientConnectionId.getConnectionId(addresses[i],
              protocol, ticket, 0, conf);
          ClientConnection connection =getConnection(remoteId, call);
          connection.sendParam(call);            // sendeach parameter
        } catch (IOException e) {
          results.size--;                        //  wait for one fewer result
        }
      }
      while(results.count != results.size) {
        try {
          results.wait();                    // wait for all results
        } catch (InterruptedException e) {}
      }
      return results.values;
    }
  }
}

public class ClientParallelResults {

   
    public synchronized void callComplete(ClientParallelCallcall) {
      values[call.index] = call.value;             // store the value
      count++;                                     // countit
      if(count == size)                           // if all values are in
        notify();                                 // thennotify waiting caller
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值