How to create a multithread program?
A thread is a unit of program execution that runs on their own thread. Multithreading is a core capabilities of the Java language which was there when the language was created. This multithreading capabilities can be said like running each program on their on CPU, although the machine only has a single CPU installed.
To create a Thread program in Java we can extends the
java.lang.Thread class and override the
run() method. But as we know that Java class can only extends from a single class, extending Thread class makes our class cannot be inherited from another class. To solve this problem an interface was introduced, the
Let see the simplified demo class below. In the program below we'll have three separated thread executions, the main thread, thread-1 and thread-2.