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
}
}
假设我有这样的事情。我将如何输入一个监听器来计算状态在再次变为 Activity 状态之前处于非 Activity 状态的时间,并且每次它回到非 Activity 状态时都执行此操作?
编辑:我需要知道 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;
}
}
我是一名优秀的程序员,十分优秀!