gpt4 book ai didi

java - 计算以毫秒为单位的差异需要时间

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

我不知道如何正确标题我的问题,因为我不确定这里出了什么问题,所以我将发布我的代码并解释我想要实现的目标..

我有一个每秒运行并发送事件的服务。例如,我需要在该过程中进行一些检查,每次检查都会在一定时间段后运行。每秒我都会获取设备属性值,每个检查2 秒我获取“currentBox”,每 5 秒我检查一次网络连接,每小时我尝试登录..

这是代码:

private void startAutoUpdate() {
long current = System.currentTimeMillis();
if (!connectivityHelper.isConnectedMobile() && !connectivityHelper.isConnectedEthernet() && !connectivityHelper.isConnectedWifi()) {
eventBus.post(new Event(new ObjectConnectivity(connectivityHelper.isOnline(), null), Event.EVENT_TYPE_CONNECTIVITY_EVENT));
}

boolean localReset = false; // maybe force was set after this started!!
if (clearETags)
localReset = true;

try {
try {
if (localReset) {
attributeValueRepository.clearETag();
attributeValueRepository.clear();
attributeValueRepository.fetchMeteoAttr();
} else if (canUpdate(getResources().getInteger(R.integer.weather_update_delay)))
attributeValueRepository.fetchMeteoAttr();
} catch (Exception e) {
Log.d(TAG, "fail to load meteo attributes");
}

attributeValueRepository.fetchAll();
Log.d("AVTEST", "Fetch all");
try {
if (localReset) {
deviceStateRepository.clearETag();
deviceStateRepository.clear();
deviceStateRepository.fetchAll();
} else if (canUpdate(getResources().getInteger(R.integer.state_update_delay)))
deviceStateRepository.fetchAll();
} catch (Exception e) {
Log.d(TAG, "fail to load deviceStateRepository");
}

try {

if ((current - lastBoxCall) > 2000) {
currentBox = restTemplate.getRemoteOnlyCopy().getForObject("v2/box", Box.class);
if (currentBox != null) {
newSyncDate = df.format(currentBox.getSyncDate());
if (syncDate == null) {
syncDate = df.format(currentBox.getSyncDate());
} else {
if (!newSyncDate.equals(syncDate)) {
eventBus.post(new Event(null, Event.EVENT_TYPE_REFRESH_REQUEST));
syncDate = newSyncDate;
}
}
}
lastBoxCall = current;
Log.d("AVTEST", "Fetch current box");
}
} catch (Exception e) {
e.printStackTrace();
}

try {
if ((current - lastNetworkCall) > 5000) {
if (connectivityHelper.isConnectedEthernet() || connectivityHelper.isConnectedWifi()) {
try {
int timeoutMs = 1500;
Socket sock = new Socket();
SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);
sock.connect(sockaddr, timeoutMs);
sock.close();
isOnline = true;
showNotification(true);
} catch (IOException e) {
isOnline = false;
showNotification(false);
}
connectivityHelper.setIsOnline(isOnline);
eventBus.post(new Event(isOnline, Event.EVENT_CONNECTION));
} else {
connectivityHelper.setIsOnline(false);
showNotification(false);
}
lastNetworkCall = current;
Log.d("AVTEST", "Check network connection");
}
} catch (Exception e) {
e.printStackTrace();
}

try {
if ((current - lastLoginCall) > 3600000) {
restTemplate.login(restTemplate.getUsername(), restTemplate.getPassword(), restTemplate.getSerial());
}
lastLoginCall = current;
Log.d("AVTEST", "Relogin");
} catch (Exception e) {
Log.e(TAG, e.getMessage() != null ? e.getMessage() : "relogin error");
}

if (localReset) {
clearETags = false;
}

errorCount = 0;
previousCall = System.currentTimeMillis();

eventBus.post(new Event(null, Event.EVENT_TYPE_AUTO_UPDATER_SERVICE));
eventBus.post(new Event(new ObjectConnectivity(connectivityHelper.isOnline(), null), Event.EVENT_TYPE_CONNECTIVITY_EVENT));

} catch (Exception e) {
Log.d(TAG, "", e);
}
}

所以重点是,我屏幕上的小部件应该在每个事件后(每秒)更新,但由于某种原因它们有时会延迟,所以我对其进行了调试。我发现此部分导致了延迟:

  try {
if ((current - lastLoginCall) > 3600000) {
restTemplate.login(restTemplate.getUsername(), restTemplate.getPassword(), restTemplate.getSerial());
}
lastLoginCall = current;
Log.d("AVTEST", "Relogin");
} catch (Exception e) {
Log.e(TAG, e.getMessage() != null ? e.getMessage() : "relogin error");
}

我在 Android Monitor 中查找,这是流程:

D/AVTEST: Fetch all
D/AVTEST: Fetch current box
D/AVTEST: Check network connection
D/AVTEST: Relogin
D/AVTEST: Home screen types update
D/AVTEST: Push logic: key: 2130968946, value: ViewHolder{3cec115 position=0 id=-1, oldPos=-1, pLpos:-1}

最后两个不是来自这个服务,但现在没关系,问题是,日志在“重新登录”之前停止大约15秒,然后继续..是计算这个东西太重还是什么?

((current - lastLoginCall) > 3600000)

最佳答案

您说日志在重新登录之前停止 15 秒左右。我最好的猜测是它是由restTemplate.login 调用引起的 - 请记住您的“重新登录”日志是在restTemplate 调用之后。这是有道理的,因为它取决于网络连接以及服务器对登录请求的响应速度。

(当前 - 最后登录)始终可以忽略不计。

关于java - 计算以毫秒为单位的差异需要时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50175021/

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