gpt4 book ai didi

java - 枚举和状态...使用监听器来计时 - Java

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:56 25 4
gpt4 key购买 nike

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;
}
}

关于java - 枚举和状态...使用监听器来计时 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com