How is Thread Implemented in java?
The thread is nothing but a single piece of program or process, for any program, there will be the main thread and some other threads. Implementing thread in java is a two steps process.
1) First of all, we have to define a function that can be executed as a thread.
2) Now, this function has to be executed as a thread.'
Explanation:
a) The signature of a function using which we implement a thread is defined in an interface by the name ‘Runnable’. This interface has one function definition public void run () for which we need to provide the body.
b) After providing the body, we need to execute this functionality as a thread (i.e. simultaneously with other parts of the program).
c) There is a class by the name Thread present in the API. This class supports much functionality which makes a given functionality get executed as a thread. One such functionality is the start () method.
d) The start () method recognizes the run () method of the Runnable interface and then the run () method is executed as a thread.
e) Thread class and Runnable interface are the two structures using which we implement Thread-based multitasking in java.