samedi 6 avril 2019

How to periodically print to terminal from an if statement that is nested in a while loop

I am writing a java application that will hash a series of input values repeatedly until a condition is met. I have accomplished this by nesting a series of if/else statements inside a while loop. I want to be able to print the hash rate to the terminal every 3 seconds while the application is actively hashing, and not repeatedly until the condition is met. I have tried using ExecutorService and scheduling a TimerTask but neither worked the way I wanted them to as they both kept on executing after the condition that should have stopped them was met. I know I am missing something but I don't know what ):

I've included a small snippet, feel free to ask for any information you may think is relevant.

Any help would be greatly appreciated!

I tried using the TimerTask like this:

while(iterator)
    if (difficulty == 1) {
        if (!hash.startsWith("0")) {
            long endTime = System.nanoTime();
            Nonce++;
            long deltaN = endTime - startTime;
            long deltaS = (deltaN / 1000000000);
            long hashRate = (Nonce / deltaS);
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                System.out.println("Current hash rate: " + hashRate + " " + "hash/s");
                }
            }, 0, 3000);
      } else {
          System.out.println("Valid hash found! " + hash);
          iterator = false;
      }
}

Aucun commentaire:

Enregistrer un commentaire