How To Create Java Threads

In this tutorial, we will learn how to create threads in Java. Here, we have examples, in which we are creating threads by extending the Thread class and by implementing the Runnable interface. By Jyoti Singh Last updated : January 26, 2024

Java Threads

Threads are lightweight processes. A process is a complete program while a thread is a small task which may or may not independent. Each java program contains a main thread which is responsible for the execution of main method.Threads is basically used for asynchronous tasks that is for background processing and to use the concept of multitasking.

Creation Ways

In Java threads can be created in two ways:

  1. By extending Thread class
  2. By implementing Runnable interface

Extending Thread Class

Thread can be created by extending the Thread class.

Example

In this example, a class named newThread extends Thread class which is an inbuilt class of java has functions like run(), stop(), start(), destroy() etc. The start method will be invoked on an object of newThread class. This thread will keep running to 10000 times as per logic.

class newThread extends Thread {
  private int threadid;
  //this is a constructor to take the arguments in a thread
  public newThread(int id) {
    //here threadid will be 1
    threadid = id;
  }
  public void run() {
    /*
    run() method is responsible for running a thread,
    all the programming logic will be contain by this thread 
    i.e., what u want your thread to do
    */
    for (int i = 0; i < 10000; i++) {
      System.out.println(threadid + " : " + i);
    }
  }
}

public class CreateThreadByExtending {
  public static void main(String[] args) {
    // creating an object of newThread class and 
    // sending 1 as an argument
    newThread thread1 = new newThread(1);
    // start() function is use to start the thread 
    thread1.start();
  }
}

Output

1 : 0
2 : 1
3 : 2
4 : 3
upto 9999

Implementing Runnable Interface

Thread can also be created by implementing runnable interface.

Example

In this example, a class named newThread1 implements a Runnable interface which is an inbuilt interface of java. Here an object will be created of class newThread1 and this object will be passed to Thread class to invoke the run method this is because objects of class newThread1 will not be considered as Thread object.

class newThread1 implements Runnable {

  public void run() {
    /*
    run() method is responsible for running a thread,
    all the programming logic will be contain by this thread 
    i.e., what u want your thread to do
    */
    System.out.println("Thread is running.........");
  }
}
public class CreateThreadByImplementing {
  public static void main(String[] args) {
    //creating an object of newThread class
    newThread1 m1 = new newThread1();
    /*
    as class newThread class doesn't extend Thread class,
    therefor its object will not be consider as
    thread object.We need to explicitily call Thread class object 
    and passing the object of newThread class that implements runnable,
    so run() method will be executed.
    */
    Thread t1 = new Thread(m1);
    //starting the thread
    t1.start();
  }
}

Output

Thread is running.......

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.