How to create a Thread in Java

In this post we will provide an example of how to create a thread in Java. A thread can be created by either extending the thread class or implementing the runnable interface.

How to create a Thread in Java

Example 1: Extending the Thread class.

The above class extends the Thread class and overrides the run method with the logic you wish to run inside your thread.

To run your thread you will need to do the following:

Example 2: Implementing the Runnable interface

An alternative approach to creating a Thread is to implement the runnable interface.

You will then need to pass your runnable object to a thread.

If the logic inside your runnable object is limited then you can use this shorthand notation.

The thread.start() method moves the Thread from the NEW state to the Runnable state and when the thread gets a chance to execute, it will execute the run() method.

In conclusion, I hope this post was useful in showing you how to create a thread in Java.

Similar posts:

  1. Java WatchService Example
  2. Java GET request using Unirest
  3. How to sort an Array in Java


  1. Java concurrency tutorial
  2. Thread API documentation
  3. Thread states documentation