Java – Custom Thread Creation

Define Custom thread in java:

  • We can define Custom thread in 2 ways
    • By extending Thread class
    • By implementing Runnable interface

Note:

  • 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.

  1. New State: Thread object created but not yet started.
  2. Runnable State: Thread waits in queue until memory allocated to run.
  3. Running State: Threads running independently once they started.
  4. Blocked State: The thread is still alive, but not eligible to run.
  5. Terminated State: A thread terminates either by complete process or by occurrence of error

Scroll to Top