Create and Start a Thread in Java

Threads are lightweight sub-processes which share the same address space and data, and can execute different tasks concurrently. Compared to processes, context switching between threads is less expensive. In Java environment, a Java thread is an instance of Thread class or its subclasses.

Create and start a thread

To create a thread
Thread thread = new Thread();

To start the thread
thread.start();

start() method is used to start a newly created thread. It performs following tasks:

  1. A new thread starts
  2. The thread moves from New state to the Runnable state.
  3. When the thread gets a chance to execute, the run() method of the target will run.

In practice, there are two ways to create a thread and execute some codes. One way is to extend Thread class, and another way is to implement Runnable interface and inject the target a thread. Both ways has a run() method. The run() method is used to perform action for a thread.
create_start_thread_Java

Extending Thread Class

The procedure to create a thread by extending Thread class is described below:

  1. Create a subclass of Thread class
  2. Overrides the run() method and add the code in run() method which will be executed by the thread.
  3. Call start() method to move the read to Runnable state

Example:

You also can create an anonymous subclass of Thread. Here is one example

Implementing Runnable Interface

The procedure to create a thread by implementing the Runnable interface is described below:

  1. Create a class that implements the Runnable interface
  2. Overrides the run() method and add the code in run() method which will be executed by the thread.
  3. Create a thread object by injecting a Runable object
  4. all start() method to move the read to Runnable state

Here is another example:

An anonymous implementation example of Runnable interface is

Implement Runnable or Extend Thread?
Normally, implements Runnable is the preferred way. There are several pros implementing Runnable.

  1. Runnable is interface, and easier to extend.
  2. In OOP world, coding to an interface rather than to implementation is still a good practice.
  3. When accessing the same resources, using Runnable implementation will consume less memory than extending Thread class. Many threads can share the Runnable.
  4. Decouple the computation from the execution.

When to extend Thread?
If you want to modify the functionality of Thread class, you can extend Thread.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditShare on StumbleUponEmail this to someoneShare on TumblrDigg this

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">