Define Custom thread in java:
- We can define Custom thread in 2 ways
- By extending Thread class
- By implementing Runnable interface
- Runnable interface has only abstract method called run().
- Thread class implement Runnable interface and override run() method – but with empty body.
- Programmer has to override run() method to define thread logic.
Thread Life Cycle: Every thread has undergone different states in its Life.
- New State: Thread object created but not yet started.
- Runnable State: Thread waits in queue until memory allocated to run.
- Running State: Threads running independently once they started.
- Blocked State: The thread is still alive, but not eligible to run.
- Terminated State: A thread terminates either by complete process or by occurrence of error