Home » Java programming language

How to stop a thread in Java?

Stopping a thread in Java: Here, we are going to learn how to stop a thread in Java programming language with examples?
Submitted by Preeti Jain, on August 06, 2019

Stopping a thread

  • As we know that there are no direct or shortcut ways to stop thread in Java.
  • As we know thread in java stops when the execution of run() method completed normally or thread stops if it raises an exception in the meanwhile of Thread completion.
  • In previous versions of Java JDK there exists a method of Thread class named "stop" but this method is deprecated in later versions so it does not support in later versions of Java.
  • Java does not provide some direct methods to stop thread in Java but there are some control methods defined in java JDK earlier versions and the names of the method are given below:
    1. stop()
    2. suspend()
    3. resume()
  • All the methods given above are deprecated so it does not support in later versions of Java.
  • We will see how to stop a thread implicitly Java JDK stops a thread implicitly if and only if either a thread run() method completes it execution normally or if a thread raises an exception in the meanwhile.
  • Now, we will see how to stop a thread manually: we can stop a thread manually in two ways:
    1. With the help of volatile boolean variable
    2. With the help of interrupt() method

Now we will elaborate on each way one by one is given below...

With the help of volatile boolean variable

  • In the first step, we will declare a volatile boolean variable in a thread.
  • In the second step initially, we will assign the value of the volatile boolean variable as true.
  • In the third step, we will define a job inside while loop and passing parameter of the volatile boolean variable in while loop and we will keep loop inside run() method. By using loop the thread will continue to run until volatile boolean variable becomes false.
  • In the fourth step, we will define another method named "stopThread()" inside a thread and in this method, we will set volatile boolean variable is set to false to stop the thread.
  • In the fifth or final step, we don’t need to do anything manually to stop the thread. To stop a thread we can simply call user-defined stopThread() method to stop the thread.

Example:

// Java program to stop a thread with the help of 
// volatile boolean variable

class ChildThread extends Thread {
    // intially assign boolean variable with value as true
    volatile boolean thread_stop = true;

    // this method is defined to stop a thread
    public void stopThread() {
        thread_stop = false;
    }

    // this loop will continue until boolean variable become false
    public void run() {
        while (thread_stop) {
            System.out.println("Child Thread Started");
        }
        System.out.println("Child Thread Ended");
    }
}

class Main {
    public static void main(String[] args) {
        ChildThread ct = new ChildThread();
        ct.start();
        try {
            Thread.sleep(100);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        // this method will be called to stop a thread
        ct.stopThread();
    }
}

Output

Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
.
.
.
.
.
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Ended

With the help of interrupt() method

We will use readymade interrupt() method to stop a thread.

When we call interrupt method on a thread object and it will assign the interrupted status of a thread and this status can be generated by the interrupted() method.

Example:

// Java program to stop a thread by using interrupt() method

class ChildThread extends Thread {
    // this loop will continue until boolean variable become false
    public void run() {
        while (!Thread.interrupted()) {
            System.out.println("Child Thread Started");
        }
        System.out.println("Child Thread Ended");
    }
}

class Main {
    public static void main(String[] args) {
        ChildThread ct = new ChildThread();
        ct.start();
        try {
            Thread.sleep(100);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        // this method will be called to interrupt a thread
        ct.interrupt();
    }
}

Output

Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
.
.
.
.
.
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Ended





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.