繁体   English   中英

Java - 如何进行此算法更正,我不知道value0和value1何时不为空且为null

[英]Java - How to make this algorithm correction, where i do not know when value0 and value1 is not empty and null

如何使这个算法更好和工作,我总是最终到reallyFail(); 而且我无法让它在循环中无限时间,最多只能持续5分钟。

// This is a heavy rendering encryption which takes sometimes 1 minute 
// or sometimes
// it takes less then 1 seconds, very random to estimate to a fix time or delay
prepareEncoding(letter);

// Now once that is done i have two values which if null or empty 
// Goal can not be accomplished
if (main.myencryption0 != null && main.myencryption1 != null) {
  // Once those value are available
  // Proceed to he Goal
  prepareToGoal(letter);
} else {

  // Value is empty or null we do not know the status
  // We try for 3 times the same thing 
  // (i think this is wrong, but could not find alternative best way)
  for (int i = 0; main.myencryption0 == null && main.myencryption1 == null && i <= 3; ++i) {
    prepareEncoding(letter); 
  }

  // Wait still for few seconds to make 100% sure
  // We can not wait more then 5 minute, because its too long.
  try {
    Thread.sleep(3000);
  } catch (InterruptedException ex) {
    ex.printStackTrace();
  }  

  // Finally again check same thing do or die
  if (main.myencryption0 != null && main.myencryption1 != null) {
    prepareToGoal(letter);
  } else {
    reallyFail(); // OK - give up, drop the ball
  }

}

您可以使用System.currentTimeMillis检查时间。 所以你可以在5分钟后离开。

long endTime = System.currentTimeMillis() + 300000; //After 5 min leave while loop

while (System.currentTimeMillis() < endTime 
  && main.myencryption0 == null
  && main.myencryption1 == null)
{
  prepareEncoding(letter); 

  try {
    Thread.sleep(1);
  } catch (InterruptedException ex) {
    ex.printStackTrace();
  }  
}

if (main.myencryption0 != null && main.myencryption1 != null) {
  prepareToGoal(letter);
} else {
  reallyFail(); // OK - give up, drop the ball
}

我有一段时间没有在java工作,所以我不知道确切的语言结构,但尝试这样的事情:

// declare how long you are willing to wait
int maxWait = 5;   // assuming in minutes

// do your prepare goal stuff here
// ...
// ...

// get the time I started
Time started = new Time();   // use the proper construct here to get the current time
Time now;

// keep trying for some amount of time
while(!(main.myencryption0 != null && main.myencryption1 != null))
{
    // wait some arbitrary amount of time
    try { Thread.Sleep(1); }
    catch (InterruptedException ex) { ex.PrintStackTrace(); }

    // too long?
    now = new Time();  // again, get what time it is right now
    if (now > started.AddMinutes(maxWait))
        break;
}

// Finally again check same thing do or die
if (main.myencryption0 != null && main.myencryption1 != null)
    prepareToGoal(letter);
else
    reallyFail(); // OK - give up, drop the ball

嗯,看起来你正在研究单线程。 Thread.sleep(3000)只是浪费了5分钟无所事事。

看看他的例子 - 指定时间后的中断线程 - 问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM