Java-如何进行此算法更正,我不知道value0和value1何时不为空
如何使这个算法更好地工作,我总是以Java-如何进行此算法更正,我不知道value0和value1何时不为空,java,Java,如何使这个算法更好地工作,我总是以reallyFail()告终。我不能让它在while循环中无限长的时间,最多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)
reallyFail()告终代码>。我不能让它在while循环中无限长的时间,最多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
}
}
//这是一个繁重的渲染加密,有时需要1分钟
//或者有时候
//估计到一个固定的时间或延迟需要不到1秒的时间,这是非常随机的
准备编码(字母);
//完成后,我有两个值,如果为null或为空
//目标无法实现
if(main.myencryption0!=null&&main.myencryption1!=null){
//一旦这些值可用
//朝着目标前进
准备目标(字母);
}否则{
//值为空或null,我们不知道状态
//我们试了三次同样的事情
//(我认为这是错误的,但找不到替代的最佳方式)
对于(int i=0;main.myencryption0==null&&main.myencryption1==null&&i您可以使用System.currentTimeMillis检查时间。因此您可以在5分钟后离开while
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
}
long-endTime=System.currentTimeMillis()+300000;//5分钟后离开while循环
while(System.currentTimeMillis()
您可以使用System.currentTimeMillis检查时间。因此,您可以在5分钟后离开while
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
}
long-endTime=System.currentTimeMillis()+300000;//5分钟后离开while循环
while(System.currentTimeMillis()
我已经有一段时间没有使用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
我已经有一段时间没有使用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
嗯,看起来你在做单线程。线程。睡眠(3000)只是浪费5分钟什么都不做
看看他的例子-嗯,看起来你正在处理单线程。线程。睡眠(3000)只是浪费5分钟什么都不做
看看他的例子-不太确定这里发生了什么。如果你在等待线程完成,为什么不直接等待线程完成而不是猜测时间呢?我认为这更合适,因为我认为我们需要看看prepareEncoding
做了什么才能提供帮助。main.myencryption0和它的朋友被prepareEncodi改变了吗ng()?如果不是,则可以将对它们的检查移动到for循环之外(例如,将放入检查它们的If语句中)。不太确定这里发生了什么。如果您正在等待线程完成,为什么不直接等待线程完成而不是猜测时间呢?我认为这更合适,因为我认为我们需要看看prepareEncoding
能做些什么来提供帮助。main.myencryption0及其朋友是否被prepareEncoding()改变了?如果没有,您可以将它们的检查移到for循环之外(例如,将它们放入检查它们的If语句中)。