Back Button
Back to Chapter
Introduction
No items found.
Collapse_Icon
Table of Contents

Let's consider a scenario, we want to execute several programs simultaneously (multitasking ). This is achieved by using the terminology Multithreading concept.

Multithreading in Java is a process of executing multiple threads simultaneously. You should use multithreading when you want to perform heavy operations without "blocking" the flow. Example in UIs where you do a heavy processing in a background thread but the UI is still active.

1. Single Thread:

Java uses threads by using a "Thread Class". A single thread is basically a lightweight and the smallest unit of processing. Thread class provides constructors and methods to create and perform operations on a thread.

2. Creating Threads

Creating threads in java is simple. There are two ways to create a thread:

i) By extending Thread class

ii) By implementing Runnable interface.

i) By extending Thread class:

Create a thread by a new class that extends the Thread class and create an instance of that class. The extending class must override the run() method which is the entry point of the new thread.

Example:

class Multithread extends Thread { //extending from Thread class
 public void run() {
   System.out.println("thread is running...");
 }
 public static void main(String args[]) {
   Multithread t = new Multithread(); // object t is created for Multithread class
   t.start(); //Thread method
 }
}
/*OUTPUT:
thread is running... */

ii) By implementing Runnable interface:

The easiest way to create a thread is to create a class that implements the runnable interface. After implementing runnable interface , the class needs to implement the run() method, which is

public void run()

  • The run() method introduces a concurrent thread into your program. This thread will end when run() returns.
  • You must specify the code for your thread inside run() method.
  • The run() method can call other methods, can use other classes, and declare variables just like any other normal method.

Example:

class Multithread implements Runnable { //implementing runnale interface
 public void run() {
   System.out.println("thread is running...");
 }

 public static void main(String args[]) {
   Multithread m = new Multithread();
   Thread t = new Thread(m); // object t is created for Thread class
   t.start(); //Thread method
 }
}
/*OUTPUT:
thread is running...  */

3. Stopping a Thread:

Whenever we want to stop a thread from running further, we may do so by calling its stop() method. This causes a thread to stop immediately and move it to its dead state. It forces the thread to stop abruptly before its completion.

syntax:

t.stop();

4. Blocking a Thread:

A thread can also be temporarily suspended or blocked from entering into the runnable and subsequently running state by using either of the following thread methods:

sleep(t); // blocked for ‘t’ milliseconds

suspend(); // blocked until resume() method is invoked

wait(); // blocked until notify () is invoked

These methods cause the thread to go into the blocked (or not-runnable) state. The thread will return to the runnable state when the specified time is elapsed in the case of sleep( ), the resume() method is invoked in the case of suspend( ), and the notify( ) method is called in the case of wait().