gpt4 book ai didi

java - 如何执行一个任务并每分钟返回一个数据

转载 作者:行者123 更新时间:2023-12-02 11:09:38 25 4
gpt4 key购买 nike

我想创建一个执行某些任务并每分钟返回列表的函数。但现在在 this 的帮助下和其他一些链接我可以打印结果但不能返回它。主要原因是 run 方法的返回类型是 void ,它没有更改为列表。

我的代码如下:

 List<String> ignoreList=new ArrayList<>();
Map<String,List<String>> deviceMap=new HashMap<>();
List<String> newlyAddedUUIDList=new ArrayList<>();

int MINUTES = 1; // The delay in minutes
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() { // Function runs every MINUTES minutes.
// Run the code you want here
String xSubjectToken=SecretIdRetreiver.getXSubjectToken();
Map<String, List<String>> uuidInfo= SecretIdRetreiver.getUUIDList(xSubjectToken); // If the function you wanted was static
System.out.println(uuidInfo);
Set<String> uuidSetList=uuidInfo.keySet();
for(String uuid: uuidSetList) {
if(ignoreList.contains(uuid)) {
System.out.println("UUid already in the ignore list");
}else {
if(!deviceMap.containsKey(uuid)) {
Map<String,List<String>> resultOfSC1=SecretIdRetreiver.performStage1Config(xSubjectToken, uuid);
System.out.println(resultOfSC1);
if(resultOfSC1.containsKey("AddInIgnoreList")) {
List<String> result=resultOfSC1.get("AddInIgnoreList");
String uuidToBeIgnored=result.get(0);
ignoreList.add(uuidToBeIgnored);
}else if(resultOfSC1.containsKey("Successful")) {
List<String> result=resultOfSC1.get("Successful");
System.out.println(result);
String deviceId=result.get(1);
// System.out.println(deviceId);
String[] deviceuUIdSplit=deviceId.split("\\.");
String duUID=deviceuUIdSplit[0];
System.out.println(duUID);
String serialNumber=result.get(0);
String secretNumber=result.get(2);
List<String> info=new ArrayList<>();
info.add(secretNumber);
info.add(serialNumber);
if(deviceMap.containsKey(duUID)) {

}else {
newlyAddedUUIDList.add(duUID);
}
deviceMap.put(duUID, info);

}
}else {
System.out.println("Already Added in the Device list");
}

}

}
getNewAddedUUId(newlyAddedUUIDList);
// System.out.println(newlyAddedUUIDList+"NewAdded UUIDs");
newlyAddedUUIDList.clear();


}


}, 0, 1000 * 60 * MINUTES);

public static List<String> getNewAddedUUId(List<String> newlyAdded) {
// TODO Auto-generated method stub
System.out.println(newlyAdded+"Newly Added UUIDs");
return newlyAdded;

}

主要目标是应将此代码放置在每分钟返回结果列表的函数内。

最佳答案

问题在于 - 函数每次调用仅返回一次 - 而不是多次(例如每分钟一次)。

因此,要么每分钟调用一次函数。

或者将列表放入某个成员变量中,每分钟告诉“调用者”去看一下。

但是现在我们已经接近监听器模式了。所以在 UI 中我们有一个按钮。我们有多个代码部分,都希望在按下按钮时收到通知 - 因此我们将那些希望作为按钮的监听器收到通知的内容添加到按钮中。然后,当按钮上发生事件时,我们可以遍历监听器列表并告诉他们事件已经发生。

此模式的稍微复杂一点的用法是在操作中使用:

https://docs.oracle.com/javase/tutorial/uiswing/misc/action.html

https://docs.oracle.com/javase/7/docs/api/javax/swing/Action.html

关于java - 如何执行一个任务并每分钟返回一个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50692914/

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