So I am trying to write a simple class that is extending Thread class. While writing it I found a funny and confusing casualty and I would be very grateful if someone explain me
why this code does work:
@Override
public void run(){
startTime = System.currentTimeMillis();
running = true;
while (isRunning()) {}
}
but this code doesn't:
@Override
public void run(){
startTime = System.currentTimeMillis();
running = true;
while (running) {}
}
with overridden interrupt method:
@Override
public void interrupt(){
running = false;
Long endTime = System.currentTimeMillis();
int time = Math.round(endTime - startTime);
out.println(time);
}
BTW isRunning()
is just a simple getter witch returns boolean value of running
.