Can anybody explain why if-else do not passing validator?
I do understand it might have less code with Thread.Sleep(1000\owner.getSpeed()
But if-else is also looking like correct decision, so what is wrong with that?
Any extra explanaition will help,thanks in advance...
package com.codegym.task.task16.task1612;
/*
Stopwatch
*/
public class Solution {
public static volatile boolean isStopped = false;
public static void main(String[] args) throws InterruptedException {
Runner usain = new Runner("Usain", 4);
Runner carl = new Runner("Carl", 2);
// On your marks!
// Get set!
// Go!
usain.start();
carl.start();
Thread.sleep(2000);
isStopped = true;
Thread.sleep(1000);
}
public static class Stopwatch extends Thread {
private Runner owner;
private int stepNumber;
public Stopwatch(Runner runner) {
this.owner = runner;
}
public void run() {
try {
while (!isStopped) {
doStep();
}
} catch (InterruptedException ignored) {
}
}
private void doStep() throws InterruptedException {
stepNumber++;
// write your code here
if(owner.getSpeed()==4)
{
System.out.println(owner.getName() + " takes step " + stepNumber + "!");
Thread.sleep(250);
}
if (owner.getSpeed()==2)
{
System.out.println(owner.getName() + " takes step " + stepNumber + "!");
Thread.sleep(500);
}
}
}
public static class Runner {
Stopwatch stopwatch;
private String name;
private int speed;
public Runner(String name, int speed) {
this.name = name;
this.speed = speed;
this.stopwatch = new Stopwatch(this);
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
public void start() {
stopwatch.start();
}
}
}