Multithreading in java watch online session on youtube in Hindi)
How to create thread in javawatch online session on youtube in Hindi)
What is runtime stack in Java?watch online session on youtube in English)
Sleep Method in Java ?watch online session on youtube in English)
What is Java Lock? Or Synchronizationwatch online session on youtube in English)
What is Thread Group in Java?watch online session on youtube in English)
Defining a thread by implementing Runnable Interface
Runnable interface is present in the java.lang package and it contains only one method run( ).
Now, a question arise i.e.
Why a thread implements Runnable Interface, because it can be used as extending Thread class.
My Answer is, because when we use the extending method in multithreading we can't take full benefits of Inheritance as if required a programmer to extend some another class or need to use another method that is not available in Thread Class in java.
Hence, it is more powerful and beneficial if you are using implementing interface methods.
Example:
Here, myRunnable class implements Runnable interface and it contains only one method run, in which a for loop is written as the job of the thread. Another class ThreadDemoImpliment is written.