I was trying to test/learn a few basic things in threading(Java) and came across a simple but confusing output. Following are my classes
public class CopyMaster implements Runnable{
@Override
public void run() {
System.out.println("Run called from Copy Master for thread "+Thread.currentThread().getName());
}
}
Main Class from where I am invoking threads
public class Main {
public static void main(String[] args) {
Thread[] threadArray = new Thread[4];
for(int i=0; i<threadArray.length; i++){
threadArray[i] = new Thread(new CopyMaster());
}
for(Thread t : threadArray){
//[Line of intrest]System.out.println("Starting Thread "+t.getName());
t.start();
}
}
}
OP(With Line of Interest un-commented)
Starting Thread Thread-0
Starting Thread Thread-1
Run called from Copy Master for thread Thread-0
Starting Thread Thread-2
Run called from Copy Master for thread Thread-1
Starting Thread Thread-3
Run called from Copy Master for thread Thread-2
Run called from Copy Master for thread Thread-3
OP(With Line of Interest commented)
Run called from Copy Master for thread Thread-2
Run called from Copy Master for thread Thread-3
Run called from Copy Master for thread Thread-0
Run called from Copy Master for thread Thread-1
I have tried running the code multiple times and observed that the sequence of print-outs are random in commented line of interest and in un-commented one its proper(in sequence)...
Why is it behaving like that?//EDITED as this confuses what i want to mean
EDIT:
I understand that threads behaves unpredictably but my main question is why it maintains oredr ALWAYS in one case and why random behavior is there in other case?
This is precisely the behaviour of multithreading in Java, unpredictable.
for(Thread t : threadArray){
//[Line of intrest]System.out.println("Starting Thread "+t.getName());
the above line runs in main thread --> so output is always in order (though other output lines can come in between)
t.start();
The above line starts each thread (prepares thread for execution, but the run method might not be called immediately). Nobody can predict which thread's run() will execute first. It is totally left to the JVM and underlying OS.
}
If you want the output to be ordered, use join() to wait until other thread terminates.
EDIT :
OP(With Line of Interest un-commented)
Starting Thread Thread-0 --> executed in main thread --> 0--> in order
Starting Thread Thread-1 --> main thread --> 1 --> in order
Run called from Copy Master for thread Thread-0 --> executed in thread-0.
Starting Thread Thread-2 --> executed in main. --> 2 -> in order
Run called from Copy Master for thread Thread-1 --> executed in thread-1
Starting Thread Thread-3 --> main --> 3 --> in order
Run called from Copy Master for thread Thread-2 -->executed in thread-2.
Run called from Copy Master for thread Thread-3 --> executed in thread-3.
Note : Starting Thread Thread-0 is printed in the main thread. So, the next line thread.start() might not even have been executed. The logs are misleading.
The below lines actually suggest how threads ran. These logs aren't misleading.
OP(With Line of Interest commented)
Run called from Copy Master for thread Thread-2
Run called from Copy Master for thread Thread-3
Run called from Copy Master for thread Thread-0
Run called from Copy Master for thread Thread-1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With