Home » Java programming language

Java ThreadGroup interrupt() method with example

ThreadGroup Class interrupt() method: Here, we are going to learn about the interrupt() method of ThreadGroup Class with its syntax and example.
Submitted by Preeti Jain, on December 23, 2019

ThreadGroup Class interrupt() method

  • interrupt() method is available in java.lang package.
  • interrupt() method is used to stop all threads in this thread group.
  • interrupt() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • interrupt() method may throw an exception at the time of interrupting threads in this thread group.

Syntax:

    public final void interrupt();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is void – it returns nothing.

Example:

// Java program to demonstrate the example 
// of void interrupt () method of ThreadGroup 

public class Interrupt implements Runnable {
    public static void main(String[] args) {
        Interrupt in = new Interrupt(); in .interrupted();
    }

    public void interrupted() {
        // Create two thread group and the named are base
        // and derived

        ThreadGroup base = new ThreadGroup("Base ThreadGroup");
        ThreadGroup derived = new ThreadGroup(base, "Derived ThreadGroup");

        // create two threads
        Thread th1 = new Thread(base, this);
        Thread th2 = new Thread(derived, this);

        // By using getName() method is to retrieve the
        // name of the thread th1
        System.out.println(th1.getName() + " " + "begins.....");

        // By using start() method is to start its execution 
        // of thread th1
        th1.start();

        //By using getName() method is to retrieve the
        // name of the thread th2
        System.out.println(th2.getName() + " " + "begins.....");


        // By using start() method is to start its execution 
        // of thread th2
        th2.start();

        try {
            th1.interrupt();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // Override run()
    public void run() {
        try {
            Thread.sleep(1000);
            System.out.println("We will wait till another thread execution");
        } catch (Exception ex) {
            System.out.println("Thread interrupted :" + ex.getMessage());
        }
    }

}

Output

Thread-0 begins.....
Thread-1 begins.....
Thread interrupted :sleep interrupted
We will wait till another thread execution





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.