2012-03-29 2 views
1

Wie man diesen Algorithmus besser macht und arbeitet, wo ich immer bis zu reallyFail(); endende bin. Und ich kann es nicht für unbegrenzte Zeit in die Schleife lassen, nur für höchstens 5 Minuten.Java - Wie man diese Algorithmuskorrektur macht, wo ich nicht weiß, wenn value0 und value1 nicht leer und null ist

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

} 
+0

was nicht funktioniert? – Woot4Moo

+1

Nicht wirklich sicher, was hier vor sich geht. Wenn du auf das Ende eines Threads wartest, warum warte nicht einfach darauf, dass der Thread beendet wird, anstatt mal zu raten? –

+0

Ich denke, es passt besser für [codereview.SE] (http://codereview.stackexchange.com/) – amit

Antwort

2

Sie können die Uhrzeit mit System.currentTimeMillis überprüfen. So können Sie die Zeit nach 5 Minuten verlassen.

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

Ich habe nicht in Java in einer Zeit lang gearbeitet, so dass ich weiß nicht, die genauen Sprachkonstrukte, aber so etwas wie dies versuche:

// 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 
Verwandte Themen