From c1e0ecaf5f40230f246c56e49a5826b753f18e62 Mon Sep 17 00:00:00 2001 From: Frederik Kammel Date: Sat, 26 Jun 2021 13:52:01 +0200 Subject: [PATCH] Fixed duration and exit of button pusher --- .../commandlineGames/games/ButtonPusher.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/github/vatbub/commandlineGames/games/ButtonPusher.java b/src/main/java/com/github/vatbub/commandlineGames/games/ButtonPusher.java index 777098c..bc3b278 100644 --- a/src/main/java/com/github/vatbub/commandlineGames/games/ButtonPusher.java +++ b/src/main/java/com/github/vatbub/commandlineGames/games/ButtonPusher.java @@ -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. @@ -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]++; } @@ -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); } @@ -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!"); } }