Skip to content

Commit

Permalink
Fixed duration and exit of button pusher
Browse files Browse the repository at this point in the history
  • Loading branch information
vatbub committed Jun 26, 2021
1 parent a5d7eaa commit c1e0eca
Showing 1 changed file with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -53,12 +53,12 @@ public void launch(Scanner in) {
String res = in.nextLine();
if (!res.equalsIgnoreCase("exit")) {

Thread inputThread = new Thread(() ->{
Thread inputThread = new Thread(() -> {
while (!end[0]) {
String res2 = in.nextLine();
if (res2.equalsIgnoreCase("exit")) {
// gracefully exit the future task
end[0] =true;
end[0] = true;
} else if (!end[0]) {
counter[0]++;
}
Expand All @@ -67,9 +67,21 @@ public void launch(Scanner in) {
inputThread.setName("inputThread");
inputThread.start();

Thread endThread = new Thread(() -> {
try {
Thread.sleep(60000);
end[0] = true;
} catch (InterruptedException e) {
FOKLogger.log(ButtonPusher.class.getName(), Level.INFO, "An error occurred.", e);
}
});
endThread.setName("endThread");
endThread.start();
}

while (!end[0]) {
try {
Thread.sleep(10000);
end[0] =true;
Thread.sleep(500);
} catch (InterruptedException e) {
FOKLogger.log(ButtonPusher.class.getName(), Level.INFO, "An error occurred.", e);
}
Expand All @@ -80,7 +92,7 @@ public void launch(Scanner in) {
} else {
FOKLogger.info(ButtonPusher.class.getName(), "You have pressed the Enter key " + counter[0] + " times!");
}
double rate = Math.round(100.0*counter[0]/60.0)/100.0;
double rate = Math.round(100.0 * counter[0] / 60.0) / 100.0;
FOKLogger.info(ButtonPusher.class.getName(), "That's a rate of " + rate + " pushes per second!");
}
}

0 comments on commit c1e0eca

Please sign in to comment.