Created
March 22, 2018 09:01
-
-
Save Brutt/264b65e2b4271da6c07aa3eb75730e22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class KeyboardReader implements Runnable { | |
private ArrayList<String> arrayList = new ArrayList<>(); | |
@Override | |
public void run() { | |
try { | |
Timer timer = new Timer("timer1", 5); | |
Thread thread = new Thread(timer); | |
thread.start(); | |
Scanner in = new Scanner(System.in); | |
String currentMessage; | |
int i = 0; | |
while (true) { | |
currentMessage = in.nextLine(); | |
arrayList.add(currentMessage); | |
if (timer.isTimerOff()) { | |
System.out.println(arrayList.toString()); | |
arrayList.clear(); | |
timer.setProceedSecond(0); | |
} | |
if(i==1) { | |
thread.join(); | |
i = 1; | |
} | |
System.out.println(timer.isTimerOff()); | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
public ArrayList<String> getArrayList() { | |
return arrayList; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class TestTimer { | |
/*public static void main(String[] args) { | |
Timer timer = new Timer("timer1", 10); | |
Timer timer2 = new Timer("timer 2", 10); | |
Timer timer3 = new Timer("timer 3", 10); | |
Thread thread = new Thread(timer); | |
Thread thread2 = new Thread(timer2); | |
Thread thread3 = new Thread(timer3); | |
thread.start(); | |
thread2.start(); | |
thread3.start(); | |
}*/ | |
public static void main(String[] args) { | |
try { | |
KeyboardReader keyboardReader = new KeyboardReader(); | |
Thread threadKey = new Thread(keyboardReader); | |
threadKey.start(); | |
threadKey.join(); | |
/*ArrayList<String> arrayList = keyboardReader.getArrayList(); | |
for (String s : arrayList) { | |
System.out.println(s); | |
}*/ | |
System.out.println("End"); | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Timer implements Runnable { | |
private String name; | |
private int proceedSecond; | |
private int generalSecond; | |
private final long delay = 1000L; | |
public Timer(String name, int generalSecond) { | |
this.name = name; | |
this.generalSecond = generalSecond; | |
} | |
@Override | |
public void run() { | |
while(!isTimerOff()) { | |
//System.out.println(name + " : " + (generalSecond - proceedSecond)); | |
proceedSecond++; | |
try { | |
Thread.sleep(delay); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
//System.out.println(name + " has finished"); | |
} | |
public boolean isTimerOff(){ | |
return proceedSecond == generalSecond ? true : false; | |
} | |
public void setProceedSecond(int proceedSecond) { | |
this.proceedSecond = proceedSecond; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment