I asked to do a excersice of threading - 3 threads that will be a "runners" in a race and clicking ENTER will finish one of the threads run and return the time that took him to run.

Therefore I did this code - 

**Class of Runner :**

```
import java.util.*;
public class Runner extends Thread {
    Date startDate;
    long startTime;    
    String name;
    private volatile boolean flag = true;

    //This method will set flag as false
    Runner(String name)
    {
        this.name = name;
    }
    public void stopRunning()
    {
        flag = false;
    }

    @Override
    public void run()
    {
        this.startDate = new Date();
        this.startTime = this.startDate.getTime();
        System.out.println(startTime);         
        while (flag)
        {
        }
        Date d = new Date();
        System.out.println(this.name + " Stopped Running....");
        System.out.println("The time that passed is " + Long.toString((d.getTime()-this.startTime)/1000) + " secondes" );
    }
}

```

**Main :**

```
import java.io.*;
public class Main {
    public static void main(String args[])
    {
        Runner run1 = new Runner("run1");
        Runner run2 = new Runner("run2");
        Runner run3 = new Runner("run3");
        run1.start();
        run2.start();
        run3.start();
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        run1.stopRunning();
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }

        run2.stopRunning();
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        run3.stopRunning();
    }
}

```

The code simply create 3 threads of Runner, than he is waiting for an ENTER input ( in my tests of the code I strangly had to get input 2 times for each ENTER instead of 1 ) after getting the ENTER input the thread stopRunning function is on and the thread stop running.

My problem is that when i run it ( on eclipse ) it tooks to java about 5 seconds to do thread.run() to all the three threads BUT when i tested it I found that if I add in the while(flag) loop on the Runner class, simple output like System.out.println(this.name) it works normally and java/eclipse compiles and runs its immidiatly

Someone know to explain me what happend or how can i slove it without adding this line?

Thanks alot,

Omer