Multithreading consists in program using different flows of execution. These flows of execution are called threads. To understand all of this let’s see what a thread is.
A thread is an independent sequential flow of execution within a process and which is associated to a sequence of instruction. To make it easier let’s see a program or process like a builder team. If they try to finish the building doing all the same part it will get longer. But if all of them do different tasks in a coordinated way, they will finish the building before. Now let’s see some about how our CPU works with thread.
Nowadays all computers have more than 1 core and usually each core can work with two threads at least. So, what does it means? If our program has just the main thread and anyone more, it will by processed by one of the threads of the CPU. Instead, if you divide the work of the program in, for example, 2 parts, if the CPU is free, both of them could be executed at the same time. It makes our program faster.
You must to know that in some cases you will have to use threads. For example, if you’ll do heavy task, like a select to a database on the cloud or download something in your application, if you do it in the main thread, the graphic interface will be blocked until the task finish. Generally, users hate that the interfaces get blocked so we have to avoid this.
How to create a thread in Java:
We have two ways to create a thread in Java. We can create a class which extends from the class Thread or we can create a class that implements the interface Runnable. In both cases, we have to redefine the function run (). This function will contain the instructions that our thread will execute. So, when we should use each one?
Extends of Thread is the easier way to make threads but we can’t use it ever. If the class where we want to create the thread extends from other class we can’t extends from Thread too. Remember that Java doesn’t allow multiple inheritance. In these cases, we have to use Runnable.
Create a real example of using threads would be so difficult now, because there’s not all the concept that you need to do multithreading programming. So, let’s see an easy example of how to do a “Hello world” program using threads.
How to create a thread with the class Thread:
This is the most basic form of a class which implements Thread. The task that you want to your thread do must be on run function. In this case, it’s just a print on screen. To use it we have to create a new object in our main class and then use the function start(). The function start() starts the execution of the thread. It means that it calls to run method.
How to create a thread with the interface Runnable:
As you see, to create a thread using Runnable is so similar to use Thread. The only difference is that an object which implements Runnable can’t call the function start(). First, we have to create our object. Then we create a Thread object using as a parameter our object. Finally we can call the function start() through the Thread object.
10 Portland Business Centre
Manor House Lane
Datchet, Berkshire, SL3 9EG