Threads in Java — Part 1
Threads in computer systems used to run multiple processes or programs on the same time by utilizing the CPU time and resources.
As well, computers should supposed to do multiple tasks without in order to get customers attraction as well increase operational efficiency themselves. We can categorize computer-based multi-tasking into 2 types.
- Process-based multi tasking
I’ll explain about this with the example. Think about this. You may do your assignment on your computer in Microsoft Word. At the same time you can play your music player to listen to songs. At the same time you can browse some videos and downloaded them.
So, think ……
If you stop the music player, will it be affect for your assignment or downloading parts.
So, that means you can have multiple processes at the same time which are running independently on your computer. That is why we called these multi-tasking as process-based multi tasking.
When we consider about the execution of these processes, you can cut the any process at any time without interrupt another.
2. Thread-based multitasking
Here, we can have one process which consists with multiple tasks inside that.
For an example, you may experience while you are using MS Word, it will provides spellings and grammar errors itself. Like here, you can have multiple tasks which are provided by one process. They are called Thread-based multitasking.
When we consider about the execution of the task, you may think that “I will get the grammar error at the same time as soon as I type my words in MS Word”. But it does not. In here, we have only one thread to perform tasks in entire word file to execute all the multi tasks. Therefore, this one thread has to to the above 2 tasks by allocating time slots for each.
For above example, let’s think about in this way. In your computer take to show you your typed text in MS Word within 1nms (This time is just declared to sake of understanding and not for real). If it has a grammar error it will show you 0.1nms after you have done your typing [art although you feel this happens in same time.
Hope you get the clear understand about the 2 types of multi-tasks extensively. Let’s see about the Multi Threads.
Multiple threads mean multiple flows of execution.
We can create computer threads in 2 different ways.
- Extend Thread class
2. Implement Runnable Interface
The execution of the thread does not depend on the way it created. But have different advantages as well disadvantages as well.
Advantages and Disadvantages of Extend Thread class and Runnable Interface..
Look at the following example.
Here, Multi National Organization has Subsidiary. Subsidiary has Branches in all over the country.
Is it must to override the run() in Thread Implementation class?
The direct answer is No. Let’s see what will happen when we are not override run() in Thread class in Java.
Here, I’ve created a Java package class ThreadIntro and it included with 2 classes called ThreadIntro and Printer class. Below figures shows these two classes and its implementations clearly.
The first code snippet shows about the ThreadInfo class with implemented methods and printing mechanism.
As you can see, there is not run() or any implemented method inside that Printer class although we instantiate an object from it. Also, you may see that there is no any error occurred in my code. It compiling and give expected result by printing “This is from main thread” in my IDE though we did not invoke run().
Let's see what happens really inside this.
When the "printer" object invoke start(), it goes to Printer class and check if it has start() to execute. But as second figure, it does not have any implemented method itself. Then, compiler checks if the parent class has this start(). According to this case parent class is the "Thread" class. So, it does have this start() and run() method as override method. I will show you them by following code snippet by navigating Java Thread Class.
So, that means although you did not called run(), you can still execute the program since your Thread class consume both run() and start() methods.
But here is the thing. If you do not override run(), then you won’t have any job to perform by thread. Because, if we have a job to be run, it has to go through the run().
Even in above example, we have only to print “This is from main method” statement. So, we do not have any other thread to execute except the main thread.
But if we use Runnable interface to implement the thread, then you must need to override the run() itself. Because, we are enforced to implement run() when dealing with Runnable.
Let’s see how actually multiple threads are working at next section. Because, most of you think that the threads are working simultaneously. But actually they are not.
Let me do small modification on my program and show you about the Thread Scheduling activities in my next article.
Hope you get clear understand about the multi tasking and multiple threads as well its implementation.
Stay Safe !!!