Thursday, April 23, 2015

How to take Thread Dump in windows

There are different ways to take thread dumps. 

Procedure 1:  Use Powershell
Procedure 2:  Use jstack

Powershell:

1. Download "PsExec.exe" and save it.

2. Go to Start->Run and type "powershell",


3. Powershell will be opened.

4. Navigate to the path where the "PsExec.exe" exist and type following command
    “PsExec.exe -s jstack PID > Dump.txt”

5. Dump file will be created.

Procedure 2:
Create sample Threaddump.java class  and run.


public class Threaddump {

/**
* @param args
*/
public static void main(String[] args) {
int count =10;
ExecCommand myExc= new ExecCommand();
while(count >0)
{
myExc.ExecCommand("jstack -F 6072 > jstack."+count);
Thread.sleep(5000);
count--;
}
}

}

Note: Change PID and file paths.
===========================================================================
 ExecCommand class

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;

public class ExecCommand {
private Semaphore outputSem;
private String output;
private Semaphore errorSem;
private String error;
private Process p;
private int exitValue;

private class InputWriter extends Thread {
 private String input;


 public InputWriter(String input) {
  this.input = input;
 }

 public void run() {
  PrintWriter pw = new PrintWriter(p.getOutputStream());
  pw.println(input);
  pw.flush();
 }
}

private class OutputReader extends Thread {
 public OutputReader() {
  try {
  outputSem = new Semaphore(1);
  outputSem.acquire();
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
 }

 public void run() {
  try {
  StringBuffer readBuffer = new StringBuffer();
  BufferedReader isr = new BufferedReader(new InputStreamReader(p
   .getInputStream()));
  String buff = new String();
  while ((buff = isr.readLine()) != null) {
   readBuffer.append(buff);
   System.out.println(buff);
  }
  output = readBuffer.toString();
  outputSem.release();
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
}

private class ErrorReader extends Thread {
 public ErrorReader() {
  try {
  errorSem = new Semaphore(1);
  errorSem.acquire();
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
 }

 public void run() {
  try {
  StringBuffer readBuffer = new StringBuffer();
  BufferedReader isr = new BufferedReader(new InputStreamReader(p
   .getErrorStream()));
  String buff = new String();
  while ((buff = isr.readLine()) != null) {
   readBuffer.append(buff);
  }
  error = readBuffer.toString();
  errorSem.release();
  } catch (IOException e) {
  e.printStackTrace();
  }
  if (error.length() > 0)
  System.out.println(error);
 }
}

public ExecCommand(String command, String input) {
 try {
  p = Runtime.getRuntime().exec(makeArray(command));
  new InputWriter(input).start();
  new OutputReader().start();
  new ErrorReader().start();
  p.waitFor();
 } catch (IOException e) {
  e.printStackTrace();
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
}

public ExecCommand(String command) {
 try {
  p = Runtime.getRuntime().exec(makeArray(command));
  new OutputReader().start();
  new ErrorReader().start();
  p.waitFor();
  exitValue=p.exitValue();
 } catch (IOException e) {
  e.printStackTrace();
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
}

public int getExitValue(){
return exitValue;
}
public String getOutput() {
 try {
  outputSem.acquire();
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
 String value = output;
 outputSem.release();
 return value;
}

public String getError() {
 try {
  errorSem.acquire();
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
 String value = error;
 errorSem.release();
 return value;
}

private String[] makeArray(String command) {
 ArrayList<String> commandArray = new ArrayList<String>();
 String buff = "";
 boolean lookForEnd = false;
 for (int i = 0; i < command.length(); i++) {
  if (lookForEnd) {
  if (command.charAt(i) == '\"') {
   if (buff.length() > 0)
   commandArray.add(buff);
   buff = "";
   lookForEnd = false;
  } else {
   buff += command.charAt(i);
  }
  } else {
  if (command.charAt(i) == '\"') {
   lookForEnd = true;
  } else if (command.charAt(i) == ' ') {
   if (buff.length() > 0)
   commandArray.add(buff);
   buff = "";
  } else {
   buff += command.charAt(i);
  }
  }
 }
 if (buff.length() > 0)
  commandArray.add(buff);

 String[] array = new String[commandArray.size()];
 for (int i = 0; i < commandArray.size(); i++) {
  array[i] = commandArray.get(i);
 }

 return array;
}
}









7 comments:

  1. I tried a online tools it parses all formats of thread dumps http://fastthread.io/

    ReplyDelete
  2. Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing.Well written article Thank You Sharing with Us pmp training Chennai | pmp training centers in Chennai | pmp training institutes in Chennai | pmp training and certification in Chennai | pmp training in velachery

    ReplyDelete
  3. Nice Information
    "Pressure Vessel Design Course is one of the courses offered by Sanjary Academy in Hyderabad. We have offer professional
    Engineering Course like Piping Design Course,QA / QC Course,document Controller course,pressure Vessel Design Course,
    Welding Inspector Course, Quality Management Course, #Safety officer course."
    Piping Design Course in India­
    Piping Design Course in Hyderabad
    Piping Design Course in Hyderabad
    QA / QC Course
    QA / QC Course in india
    QA / QC Course in Hyderabad
    Document Controller course
    Pressure Vessel Design Course
    Welding Inspector Course
    Quality Management Course
    Quality Management Course in india
    Safety officer course

    ReplyDelete
  4. Nice Blog
    Yaaron Studios is one of the rapidly growing editing studios in Hyderabad. We are the best Video Editing services in Hyderabad. We provides best graphic works like logo reveals, corporate presentation Etc. And also we gives the best Outdoor/Indoor shoots and Ad Making services.
    video editors studio in hyderabad
    short film editors in hyderabad
    corporate video editing studio in hyderabad
    ad making company in hyderabad

    ReplyDelete
  5. BA Revaluation Result 2019
    Hey Nice Blog!! Thanks For Sharing!!! Wonderful blog & good post. It is really very helpful to me, waiting for a more new post. Keep Blogging

    ReplyDelete
  6. CnX Player offers a fantastic Video Casting feature that gives an absolute freedom to all its users to cast ANY (literally ANY) video format and ANY video codec from PC to TV within a fraction of seconds! It is a Best Media Player.

    Features @ CnX Player - Best Media Player

    * Stream ANY video from PC to Chromecast
    * Cast ALL Videos From PC To All Smart TV
    * Stream ANY video from PC to Chromecast
    * Cast Videos from iPhone iPad to Firestick TV
    * Stream any videos from PC to MI TV
    * Cast ALL Videos from PC to Android TV
    * Stream ANY Video From PC To SAMSUNG TV

    For more information visit our website at Best Media Player - CnX Player

    ReplyDelete