Threads in Java — Part 2 (Thread Scheduling)
As mentioned before, the threads in computer systems are not working parallelly as it seems. There is special function happening in side computer systems by allocating each thread for queue and let them to use resources of processor. It is called Thread scheduling.
Let’s see following example to see how it execute. Check whether is it execute as you think.
In here I add different for loops for both ThreadInfo and Printer class inside main method and run method respectively. In here, you may think that first it executed something like this.
Child:0
Child:1
Child:2
.
.
.
.
Child:9
Main:0
Main:1
Main:2
.
.
.
.
Main: 9
But let see what will be the outputs when I compile the same program 2–3 time. Also, you here it only provides nearly up to 30 lines of execution
So you see, this is exactly what is happening in thread scheduling.
We can not predict what will be the order of output. It depends on the JVM and Operating system. For an instance, if you run this program in Mac environment, then it will give you different order of output or same.
Let’s discuss what is the reason for this arbitrary order.
When we invoke start() in ThreadInfo class we can see it called the start() of Thread class (I clearly mentioned about this Java Thread-Part 1 article). In the start(), it checks the status of the thread. Then add that thread in to the group [group.add(this)] (You can see these methods exactly via a code snippet in my Threads in Java-Part 1 article). Then is started to execute and occasionally override the run() as well. The reason to get different order of output is based on the behavior of this adding part. So, the scheduler might add the thread according to the First In First Out, Round-Robin or etc.
Let’s do another small change over my code in main method. Instead of invoking start(), let’s invoke directly run() here as following.
If we compile this program at the beginning, you will get the compiled outputs from the child thread and then execute the main thread. Reason behind that is when we invoke the run(), it will always execute the child before execute main. Run() does not allow to create new thread. Then, it will execute the main method sequentially as its order.
Can we override the start() in Thread class?
Yes you can. Let’s see what happens when you do so. In here I override start() in Printer class.
In here, I created a start() inside printer class and invoke this method from the ThreadInfo. Then I compile this program, this is the output I have obtained.
So, the output does not consists with any output from the run(). The reason is, when printer object invoke start(), it does consists with the Printer class itself. Then, any new thread is not generated because start() block you to access start() resides in thread class and thereby block you to create another thread. Then it does not allowed to run Printer class run().
But there is a solution we can used to avoid this problem.
In the start(), you can invoke start method in Thread class by invoking super.start() prior to the printing statement. Then it will go to the start() of Thread class and create another thread and add to the group to be executed. So, now we have 2 thread one from main thread and another is this newly created thread and both are executing parallelly. Therefore, we can have output from child also.
Can we overload the run()?
Yes we can. But when we overload it, the start() in thread class lead to invoke the run() with no argument.
Does program terminate when the main method finish its execution?
Let’s say our child thread has more tasks to do than main thread. So, does main method not allow child thread to finish its execution?
In child class we told to print 10 threads while the main thread to print only 5 threads.
Let’s see the output:
No. As you can see after printing Main main9 it will print child class thread. So, that means termination of main thread does not always affect for the termination of child threads.
But if a child thread is a daemon thread, then after termination of main thread the rest of child threads will not be executed.
Hope you get a proper understand regarding the different behaviors of the multiple treads. Let’s discuss about the java-thread more in my next article.
Stay Safe !!!
References:
https://www.youtube.com/watch?v=h3DRYj0ZB0o&list=PLD-mYtebG3X99o6vJ3uR5P6UcH3MQSWBH&index=2