Original Post
I'm just learning multithreading, could someone please explain why i get different outputs each time I run this? I understand that the three Free objects in the main method are running on separate threads at the same time, which is why the hello worlds show up first in the console. However, why do sometimes they all show up before the outputs from the Free class and sometimes one will be first and the others after it prints Hello 0, etc. Why is it different every time? i know it probably doesnt make much difference but could someone just clear it up for me?
Thanks for any help!!
Thanks for any help!!
public class Free extends Thread{
@Override
public void run() {
for(int i = 0; i<10; i++){
System.out.println("Hello " +i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Practice {
public static void main (String args []){
Tester free = new Free();
Free free2 = new Free();
Free free3 = new Free();
free.start();
free2.start();
free3.start();
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
}
}