Java -Multithreading basics with example

The process of executing multiple threads simultaneously is known as multithreading.

Threads are lightweight sub-processes, they share the common memory space. In Multithreaded environment, programs that are benefited from multithreading, utilize the maximum CPU time so that the idle time can be kept to minimum.

The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.

Life Cycle of a Thread

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.

NEW – A thread that has not yet started is in this state.
RUNNABLE – A thread executing in the Java virtual machine is in this state.
WAITING – A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING – A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED – A thread that has exited is in this state.


The Thread class defines several methods that help manage threads:

Method           Meaning

getName         Obtain thread’s name
getPriority       Obtain thread’s priority
isAlive             Determine if a thread is still running
join                 Wait for a thread to terminate
run                  Entry point for the thread
sleep               Suspend a thread for a period of time
start                Start a thread by calling its run method


Creating a thread in Java by implementing Runnable interface

java.lang.Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread.


  1. The class should implement the Runnable interface
  2. The class should implement the run() method in the Runnable interface
  3. The functionality that is expected by the Thread to be executed is put in the run() method

Example :



public class RunnableDemoKnowledgeFactory {

public static void main(String[] args) {
System.out.println("Main thread is- " + Thread.currentThread().getName());
Thread t1 = new Thread(new RunnableDemoKnowledgeFactory().new RunnableImpl());
t1.start();
}

private class RunnableImpl implements Runnable {

public void run() {
System.out.println(Thread.currentThread().getName() + ", executing run() method!");
}
}
}


Output:

Main thread is- main
Thread-0, executing run() method!



Create a Thread by Extending a Thread Class

The second way to create a thread is to create a new class that extends Thread class. This approach provides more flexibility in handling multiple threads created using available methods in Thread class.

  1. The class should extends Thread class
  2. We will need to override run( ) method available in Thread class. This method provides an entry point for the thread and you will put your complete business logic inside this method.
  3. Once Thread object is created, you can start it by calling start() method, which executes a call to run( ) method.

Example :



class KnowledgeFactoryThreadDemo extends Thread {

public void run() {
System.out.println("thread is running...");
}

public static void main(String args[]) {
KnowledgeFactoryThreadDemo t1 = new KnowledgeFactoryThreadDemo();
t1.start();
}
}


This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments