public class X {

    private long timeInactive = 0;
    private final long timeStarted = System.currentTimeMillis();

    private enum State {
        ACTIVE,INACTIVE
    };
    private State getState() {
        if(Math.random <= 0.1) {
            return State.ACTIVE;
        }
        return State.INACTIVE;
    }
    public static void main(String[] args) {
        //code
    }
}


假装我有这样的事情。我将如何输入一个监听器,该监听器会在状态再次变为活动状态之前处于不活动状态的时间,并在状态回到不活动状态时进行此操作?

编辑:我需要知道getState()在被随机或外部因素更改后何时更改了值。例如,在psvm中,我可能会有类似的内容:

    public static void main(String[] args) {
    while(true) {
        if(value of getState() has changed from inactive to active) {
            System.out.println(How long it was inactive prior);
        }
    }
}


谢谢!

最佳答案

在没有Listner的情况下将时间保留在X

public class X {

  private long timeInactive = 0; // total time of being inactive

  private long timeLastChanged = System.currentTimeMillis();
  private State currentState = State.INACTIVE;

  enum State {
    ACTIVE, INACTIVE
  }

  public State getState() {
    changeState(); // It shouldn't be called here. I just need to simulate the state changing inside randomly
    return currentState;
  }

  private void changeState() {
    State oldState = currentState;
    if (Math.random() <= 0.1) {
      currentState = State.ACTIVE;
    } else {
      currentState = State.INACTIVE;
    }
    long now = System.currentTimeMillis();
    if (oldState == State.INACTIVE && currentState == State.ACTIVE) {
      long elapse = now - timeLastChanged;
      timeInactive += elapse;
    }
    if (oldState != currentState) {
      timeLastChanged = now;
    }
  }

  public long getTotalTimeInActive() {
    return timeInactive;
  }


  public static void main(String[] args) throws InterruptedException {
    X x = new X();
    while (true) {
      x.getState();
      System.out.println(x.getTotalTimeInActive());
      Thread.sleep(1000);
    }
  }
}


与听众:

//Listner.java
interface Listener {
  void onStateChange(X2.State currentState);
}

//X2.java
import java.util.ArrayList;
import java.util.List;

public class X2 {
  private State currentState = State.INACTIVE;

  private List<Listener> listeners = new ArrayList<>();

  enum State {
    ACTIVE, INACTIVE
  }

  public State getState() {
    changeState(); // It shouldn't be called here. I just need to simulate the state changing inside randomly
    return currentState;
  }

  private void changeState() {
    if (Math.random() <= 0.1) {
      currentState = State.ACTIVE;
    } else {
      currentState = State.INACTIVE;
    }
    for(Listener listener:listeners){
      listener.onStateChange(currentState);
    }
  }

  public void registerListener(Listener listener){
    listeners.add(listener);
  }

  public static void main(String[] args) throws InterruptedException {
    X2 x = new X2();
    TimeInactiveListener timeInactiveListener = new TimeInactiveListener();
    x.registerListener(timeInactiveListner);
    while (true) {
      x.getState();
      System.out.println(timeInactiveListner.getTimeInactive());
      Thread.sleep(1000);
    }
  }
}

class TimeInactiveListener implements  Listener {
  private long timeLastChanged = System.currentTimeMillis();
  private X2.State oldState = X2.State.INACTIVE;
  private long timeInactive = 0; // total time of being inactive
  @Override
  public void onStateChange(X2.State currentState) {
    long now = System.currentTimeMillis();
    if (oldState == X2.State.INACTIVE && currentState == X2.State.ACTIVE) {
      long elapse = now - timeLastChanged;
      timeInactive += elapse;
    }
    if (oldState != currentState) {
      timeLastChanged = now;
    }
    oldState = currentState;
  }

  public long getTimeInactive() {
    return timeInactive;
  }
}

10-07 19:45
查看更多