Exemplo n.º 1
0
    public void run() {
      Lib.debug('t', "### Try to listen from " + KThread.currentThread().toString());

      int res = comm.listen();

      Lib.debug('t', "### listened " + res + " from " + KThread.currentThread().toString());
    }
  /** Tests whether this module is working. */
  public static void runTest() {
    System.out.println("**** Communicator testing begins ****");

    /* Create a random number generator */
    Random rng = new Random();

    /* Create the communicator on which RVThreads communicate */
    Communicator comm = new Communicator();

    /* Create the communicator for listening to terminations */
    Communicator commFinished = new Communicator();

    /* Create rendezvous threads and fork them */
    KThread rvs[] = new KThread[numRVThreads];
    for (int i = 0; i < numRVThreads; i++) {
      if (i % 2 == 0) {
        /* Creating a speaker */
        rvs[i] =
            new KThread(
                new RVThread("RV-Thread(speaker) #" + i, comm, true, howMany, commFinished, rng));
        rvs[i].setName("RV-Thread(speaker) #" + i);
      } else {
        /* Creating a listener */
        rvs[i] =
            new KThread(
                new RVThread("RV-Thread(listener) #" + i, comm, false, howMany, commFinished, rng));
        rvs[i].setName("RV-Thread(listener) #" + i);
      }
      /* fork() */
      rvs[i].fork();
    }

    /*
     * Wait for all threads to signal that they're done, which is done via a Communicator for good
     * measures
     */
    for (int i = 0; i < numRVThreads; i++) {
      commFinished.listen();
      System.out.println("Acknowledged one thread exit.");
    }

    System.out.println("**** Communicator testing ends ****");
  }