public class Threading extends Thread {
@Override
public void run(){
for (int i=0;i<=5;i++){
System.out.println(Thread.currentThread().getName()+" reached "+i+" metres");
if(i==5){
System.out.println("WINNER IS "+Thread.currentThread().getName().toUpperCase(Locale.ENGLISH));
break;
}
}
}
public static void main(String[] args) {
Threading t1=new Threading();
Threading t2=new Threading();
Threading t3=new Threading();
t1.setName("Mohit");
t2.setName("Hary");
t3.setName("Himanshu");
t1.start();
t2.start();
t3.start();
}
}
I want to print the name of the thread which reaches the 5 meters first and then go out of the loop. But what is happening with this code is that the the for loop is running until each thread reaches 5 meters and printing the name of all the threads as they reach the 5 meters. Please tell me why is this happening and how can i correct it ?