Esempio n. 1
0
  void lockParticipant(final Participant p, final CoordinationImpl c) {
    synchronized (participants) {
      // wait for participant to be released
      long cutOff = System.currentTimeMillis() + participationTimeOut;
      long waitTime =
          (participationTimeOut > 500) ? participationTimeOut / 500 : participationTimeOut;
      CoordinationImpl current = participants.get(p);
      while (current != null && current != c) {
        if (current.getThread() == c.getThread()) {
          throw new CoordinationException(
              "Participant "
                  + p
                  + " already participating in Coordination "
                  + current.getId()
                  + "/"
                  + current.getName()
                  + " in this thread",
              c,
              CoordinationException.DEADLOCK_DETECTED);
        }

        try {
          participants.wait(waitTime);
        } catch (InterruptedException ie) {
          throw new CoordinationException(
              "Interrupted waiting to add Participant "
                  + p
                  + " currently participating in Coordination "
                  + current.getId()
                  + "/"
                  + current.getName()
                  + " in this thread",
              c,
              CoordinationException.LOCK_INTERRUPTED);
        }

        // timeout waiting for participation
        if (System.currentTimeMillis() > cutOff) {
          throw new CoordinationException(
              "Timed out waiting to join coordinaton", c, CoordinationException.UNKNOWN);
        }

        // check again
        current = participants.get(p);
      }

      // lock participant into coordination
      participants.put(p, c);
    }
  }