Home » Java programming language

Java Thread Class static Map getAllStackTraces() method with Example

Java Thread Class static Map getAllStackTraces() method: Here, we are going to learn about the static Map getAllStackTraces() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 18, 2019

Thread Class static Map getAllStackTraces()

  • This method is available in package java.lang.Thread.getAllStackTraces().
  • This method is used to return a Map of stack traces for all the existing or live threads.
  • This method returns a Map and Map include two factors one is key and other is value. So key represents thread and value is an array of elements of StackTrace that denotes the stack dump of the corresponding thread.
  • This method is static so this method is accessible with classname too like Thread.getAllStackTraces().
  • The return type of this method is Map so it returns an array of stack trace elements of the desired or corresponding thread.
  • This method raises an exception if check access permission denies getting a stack trace of the thread.

Syntax:

    static Map getAllStackTraces(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Thread.

Return value:

The return type of this method is Map which contains thread and array of stack trace elements of the corresponding thread.

Java program to demonstrate example of getAllStackTraces() method

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/
import java.lang.Thread;
import java.util.Map;

class GetAllStackTraces extends Thread {
    // We are overriding run() method of Thread class 
    // in  GetAllStackTraces
    public void run() {
        System.out.println("We are in GetAllStackTraces run() method");
    }

    public static void main(String[] args) {
        // Creating a thread object of GetAllStackTraces
        GetAllStackTraces gast = new GetAllStackTraces();

        // Creating a Thread class object and pass 
        // GetAllStackTraces gast object into it .
        Thread th = new Thread(gast);

        /*  Call start() method of thread class and 
            then thread class start() will call run() 
            of GetAllStackTraces
        */
        th.start();

        // Return a map of stack traces of the corresponding thread
        Map m = Thread.getAllStackTraces();
    }
}

Output

E:\Programs>javac GetAllStackTraces.java

E:\Programs>java GetAllStackTraces
We are in GetAllStackTraces run() method


Comments and Discussions!

Load comments ↻





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