This is some basic program that I trying to build to learn some Java before I'm starting in
programming courses. I can't get the time to pulloff in the right time interval between Start = Instant.now()
and Ends = Instant.now()
.
But the code works if I use Scanner instead(example: I use a String in the If(String.isEmpty() != Thread.alive())
, why does it do that? But then I get a problem with I need to press enter twice, which I makes me so so so frustrated.
class UserThread extends Thread {
public void run() {
Random rng = new Random();
int time = rng.nextInt(5000);
for(int i=1; i <= 1; i++) {
try {
Thread.sleep(time); // sleep/stop a thread for 1 second
System.out.println("GO!");
}
catch(InterruptedException e ) {
System.out.println("Error ");
}
}
}
}
public class reaktion{
public static void main(String[] args) throws IOException {
UserThread ut = new UserThread();
ut.start();
System.out.println("Ready, set....");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
if (reader.ready() && ut.isAlive()) {
reader.read();
System.out.println("Too early");
}
if (reader.ready() != ut.isAlive()) {
Instant starts = Instant.now();
reader.read();
Instant ends = Instant.now();
System.out.print("Finish! Reaction time : " + Duration.between(starts, ends).toMillis()+"ms");
}
}
}