/* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  public void run() {
    try {
      myHandle.call(seed, times, count);

      myHandle.dispose();
    } catch (GrpcException e) {
      e.printStackTrace();
    }
  }
  public static void main(String args[]) {
    try {
      if (args.length != 3) {
        System.err.println("Usage: java PIHandleArray configFile nSampling serverName");
        System.exit(1);
      }
      String configName = args[0];
      int nSampling = Integer.parseInt(args[1]);
      String hostname = args[2];
      /* create GrpcClient */
      GrpcClient client = GrpcClientFactory.getClient("org.apgrid.grpc.ng.NgGrpcClient");
      /* activate GrpcClient */
      client.activate(configName);

      /* create GrpcHandle */
      GrpcFunctionHandle[] handle = null;
      /* specify hostname */
      Properties prop = new Properties();
      prop.put(NgGrpcHandleAttr.KEY_HOSTNAME, hostname);

      handle = client.getFunctionHandles(targetExe, prop, nJobs);

      /* call function */
      PIHandleArray[] piArray = new PIHandleArray[nJobs];
      Thread[] piThread = new Thread[nJobs];
      for (int i = 0; i < nJobs; i++) {
        piArray[i] = new PIHandleArray(handle[i], i, nSampling);
        piThread[i] = new Thread(piArray[i]);
        piThread[i].start();
      }

      /* wait for finish, then calc result */
      long sum = 0;
      for (int i = 0; i < nJobs; i++) {
        piThread[i].join();
        sum += piArray[i].count[0];
      }
      double pi = 4.0 * (sum / (double) nSampling);

      /* print result */
      System.out.println("PI = " + pi);

      /* deactivate client */
      client.deactivate();
    } catch (NumberFormatException e) {
      System.err.println("Invalid argument: " + args[1]);
      System.exit(1);
    } catch (GrpcException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }