Create Thread in Java | Core Java Tutorials
This “Create Thread in Java” explains clearly that how to create thread in java application either by extending from Thread class or by using Runnable interface belongs to lang package.
Creating a Thread:
- java.lang package providing pre-defined class and interface to create User threads.
- A thread can be created in 2 ways…
- By extending from java.lang.Thread class
- By implementing from java.lang.Runnable interface
- Every Thread class must override “public void run() ” method in it’s definition.
- run() method contains the logic that executes in parallel.
- Pre-defined run() method is having empty definition.
When we use Runnable interface?
- Thread can be created in 2 ways.
- It is easy to create thread by extending from Thread class.
- We can apply thread behavior to class easily by extending from Thread.
- But if we want to apply thread behavior to Child in “Is-A relation”, we use Runnable interface.