APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.internal.vm

public Class ThreadDumper

extends Object
Class Inheritance
Imports
java.io.BufferedWriter, .ByteArrayOutputStream, .IOException, .OutputStream, .OutputStreamWriter, .UncheckedIOException, .Writer, java.nio.charset.StandardCharsets, java.nio.file.FileAlreadyExistsException, .Files, .OpenOption, .Path, .StandardOpenOption, java.time.Instant, java.util.ArrayDeque, .Arrays, .Deque, .Iterator, .List, .Objects

Thread dump support. This class defines static methods to support the Thread.dump_to_file diagnostic command and the HotSpotDiagnosticMXBean.dumpThreads API. It defines methods to generate a thread dump to a file or byte array in plain text or JSON format.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ThreadDumper.BoundedByteArrayOutputStream

A ByteArrayOutputStream of bounded size.

private static class
ThreadDumper.JsonFormat

JSON is schema-less and the thread dump format will evolve over time.

private static class
ThreadDumper.JsonWriter

Simple JSON writer to stream objects/arrays to a TextWriter with formatting.

private static class
ThreadDumper.TextWriter

Simple Writer implementation for printing text.

Field Summary

Modifier and TypeField and Description
private static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static String
decorateObject(Object obj)

Returns the identity string for the given object in a form suitable for the plain text format thread dump.

private static boolean
private static boolean

Returns:

true if the thread dump was written, false otherwise
dumpThread
(Thread thread, ThreadDumper.JsonWriter jsonWriter)

Write a thread to the given JSON writer.

public static byte[]

Returns:

the UTF-8 encoded thread dump or message to return to the tool user
dumpThreads
(String
the file path to the file, null or "-" to return a byte array
file
,
boolean
true to overwrite an existing file
okayToOverwrite
)

Generate a thread dump in plain text format to a file or byte array, UTF-8 encoded.

public static void
dumpThreads(OutputStream out)

Generate a thread dump in plain text format to the given output stream, UTF-8 encoded.

private static void
dumpThreads(ThreadDumper.TextWriter writer)

Generate a thread dump in plain text format to the given text stream.

private static void
private static void
dumpThreads(ThreadContainer container, ThreadDumper.JsonWriter jsonWriter)

Write a thread container to the given JSON writer.

private static byte[]
dumpThreadsToByteArray(boolean json, int maxSize)

Generate a thread dump in plain text or JSON format to a byte array, UTF-8 encoded.

private static byte[]
dumpThreadsToFile(String file, boolean okayToOverwrite, boolean json)

Generate a thread dump in plain text or JSON format to the given file, UTF-8 encoded.

public static byte[]

Returns:

the UTF-8 encoded thread dump or message to return to the tool user
dumpThreadsToJson
(String
the file path to the file, null or "-" to return a byte array
file
,
boolean
true to overwrite an existing file
okayToOverwrite
)

Generate a thread dump in JSON format to a file or byte array, UTF-8 encoded.

public static void
dumpThreadsToJson(OutputStream out)

Generate a thread dump in JSON format to the given output stream, UTF-8 encoded.

private static void
dumpThreadsToJson(ThreadDumper.TextWriter textWriter)

Generate a thread dump to the given text stream in JSON format.

private static long
processId()

Returns the process ID or -1 if not supported.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

MAX_BYTE_ARRAY_SIZEback to summary
private static final int MAX_BYTE_ARRAY_SIZE

Constructor Detail

ThreadDumperback to summary
private ThreadDumper()

Method Detail

decorateObjectback to summary
private static String decorateObject(Object obj)

Returns the identity string for the given object in a form suitable for the plain text format thread dump.

dumpThreadback to summary
private static boolean dumpThread(Thread thread, ThreadDumper.TextWriter writer)
dumpThreadback to summary
private static boolean dumpThread(Thread thread, ThreadDumper.JsonWriter jsonWriter)

Write a thread to the given JSON writer.

Returns:boolean

true if the thread dump was written, false otherwise

Exceptions
UncheckedIOException:
if an I/O error occurs
dumpThreadsback to summary
public static byte[] dumpThreads(String file, boolean okayToOverwrite)

Generate a thread dump in plain text format to a file or byte array, UTF-8 encoded. This method is invoked by the VM for the Thread.dump_to_file diagnostic command.

Parameters
file:String

the file path to the file, null or "-" to return a byte array

okayToOverwrite:boolean

true to overwrite an existing file

Returns:byte[]

the UTF-8 encoded thread dump or message to return to the tool user

dumpThreadsback to summary
public static void dumpThreads(OutputStream out) throws IOException

Generate a thread dump in plain text format to the given output stream, UTF-8 encoded. This method is invoked by HotSpotDiagnosticMXBean.dumpThreads.

Exceptions
IOException:
if an I/O error occurs
dumpThreadsback to summary
private static void dumpThreads(ThreadDumper.TextWriter writer)

Generate a thread dump in plain text format to the given text stream.

Exceptions
UncheckedIOException:
if an I/O error occurs
dumpThreadsback to summary
private static void dumpThreads(ThreadContainer container, ThreadDumper.TextWriter writer)
dumpThreadsback to summary
private static void dumpThreads(ThreadContainer container, ThreadDumper.JsonWriter jsonWriter)

Write a thread container to the given JSON writer.

Exceptions
UncheckedIOException:
if an I/O error occurs
dumpThreadsToByteArrayback to summary
private static byte[] dumpThreadsToByteArray(boolean json, int maxSize)

Generate a thread dump in plain text or JSON format to a byte array, UTF-8 encoded. This method is the implementation of the Thread.dump_to_file diagnostic command when a file path is not specified. It returns the thread dump and/or message to send to the tool user.

dumpThreadsToFileback to summary
private static byte[] dumpThreadsToFile(String file, boolean okayToOverwrite, boolean json)

Generate a thread dump in plain text or JSON format to the given file, UTF-8 encoded. This method is the implementation of the Thread.dump_to_file diagnostic command. It returns the thread dump and/or message to send to the tool user.

dumpThreadsToJsonback to summary
public static byte[] dumpThreadsToJson(String file, boolean okayToOverwrite)

Generate a thread dump in JSON format to a file or byte array, UTF-8 encoded. This method is invoked by the VM for the Thread.dump_to_file diagnostic command.

Parameters
file:String

the file path to the file, null or "-" to return a byte array

okayToOverwrite:boolean

true to overwrite an existing file

Returns:byte[]

the UTF-8 encoded thread dump or message to return to the tool user

dumpThreadsToJsonback to summary
public static void dumpThreadsToJson(OutputStream out) throws IOException

Generate a thread dump in JSON format to the given output stream, UTF-8 encoded. This method is invoked by HotSpotDiagnosticMXBean.dumpThreads.

Exceptions
IOException:
if an I/O error occurs
dumpThreadsToJsonback to summary
private static void dumpThreadsToJson(ThreadDumper.TextWriter textWriter)

Generate a thread dump to the given text stream in JSON format.

Exceptions
UncheckedIOException:
if an I/O error occurs
processIdback to summary
private static long processId()

Returns the process ID or -1 if not supported.

jdk.internal.vm back to summary

private Class ThreadDumper.BoundedByteArrayOutputStream

extends ByteArrayOutputStream
Class Inheritance

A ByteArrayOutputStream of bounded size. Once the maximum number of bytes is written the subsequent bytes are discarded.

Field Summary

Modifier and TypeField and Description
pack-priv final int
Inherited from java.io.ByteArrayOutputStream:
bufcount

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.ByteArrayOutputStream.close.

Implements java.io.Closeable.close.

Closing a ByteArrayOutputStream has no effect.

public void
write(int b)

Overrides java.io.ByteArrayOutputStream.write.

Writes the specified byte to this ByteArrayOutputStream.

public void
write(byte[] b, int off, int len)

Overrides java.io.ByteArrayOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream.

Inherited from java.io.ByteArrayOutputStream:
resetsizetoByteArraytoStringtoStringtoStringtoStringwriteByteswriteTo

Field Detail

maxback to summary
pack-priv final int max

Constructor Detail

BoundedByteArrayOutputStreamback to summary
pack-priv BoundedByteArrayOutputStream(int max)

Method Detail

closeback to summary
public void close()

Overrides java.io.ByteArrayOutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.ByteArrayOutputStream.close.

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Annotations
@Override
writeback to summary
public void write(int b)

Overrides java.io.ByteArrayOutputStream.write.

Doc from java.io.ByteArrayOutputStream.write.

Writes the specified byte to this ByteArrayOutputStream.

Annotations
@Override
writeback to summary
public void write(byte[] b, int off, int len)

Overrides java.io.ByteArrayOutputStream.write.

Doc from java.io.ByteArrayOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream.

Annotations
@Override
jdk.internal.vm back to summary

private Class ThreadDumper.JsonFormat

extends Object
Class Inheritance

JSON is schema-less and the thread dump format will evolve over time. HotSpotDiagnosticMXBean.dumpThreads links to a JSON file that documents the latest/current format. A system property can be used to generate the thread dump in older formats if necessary.

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
pack-priv static final int
pack-priv static final int
private static final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static int
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

JSON_FORMATback to summary
private static final int JSON_FORMAT
JSON_FORMAT_LATESTback to summary
private static final int JSON_FORMAT_LATEST
JSON_FORMAT_V1back to summary
pack-priv static final int JSON_FORMAT_V1
JSON_FORMAT_V2back to summary
pack-priv static final int JSON_FORMAT_V2
JSON_FORMAT_VERSION_PROPback to summary
private static final String JSON_FORMAT_VERSION_PROP

Constructor Detail

JsonFormatback to summary
private JsonFormat()

Method Detail

formatVersionback to summary
pack-priv static int formatVersion()
jdk.internal.vm back to summary

private Class ThreadDumper.JsonWriter

extends Object
Class Inheritance

Simple JSON writer to stream objects/arrays to a TextWriter with formatting. This class is not intended to be a fully featured JSON writer.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final boolean
private final Deque<ThreadDumper.JsonWriter.Node>
private final ThreadDumper.TextWriter

Constructor Summary

AccessConstructor and Description
pack-priv
JsonWriter(ThreadDumper.TextWriter writer, boolean generateLongsAsString)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
endArray()

End of array.

private void
endObject(boolean isArray)

End of object or array.

pack-priv void
endObject()

End of object.

private static String
escape(String value)

Escape any characters that need to be escape in the JSON output.

private void
pack-priv void
startArray(String name)

Start named array.

private void
startObject(String name, boolean isArray)

Start of object or array.

pack-priv void
startObject(String name)

Start named object.

pack-priv void
startObject()

Start unnamed object.

pack-priv void
writeLongProperty(String name, long value)

Write a property with a long value.

pack-priv void
writeProperty(String
the property name, null for an unnamed property
name
,
Object
the value or null
obj
)

Write a property.

pack-priv void
writeProperty(Object obj)

Write an unnamed property.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

generateLongsAsStringback to summary
private final boolean generateLongsAsString
stackback to summary
private final Deque<ThreadDumper.JsonWriter.Node> stack
writerback to summary
private final ThreadDumper.TextWriter writer

Constructor Detail

JsonWriterback to summary
pack-priv JsonWriter(ThreadDumper.TextWriter writer, boolean generateLongsAsString)

Method Detail

endArrayback to summary
pack-priv void endArray()

End of array.

endObjectback to summary
private void endObject(boolean isArray)

End of object or array.

endObjectback to summary
pack-priv void endObject()

End of object.

escapeback to summary
private static String escape(String value)

Escape any characters that need to be escape in the JSON output.

indentback to summary
private void indent()
startArrayback to summary
pack-priv void startArray(String name)

Start named array.

startObjectback to summary
private void startObject(String name, boolean isArray)

Start of object or array.

startObjectback to summary
pack-priv void startObject(String name)

Start named object.

startObjectback to summary
pack-priv void startObject()

Start unnamed object.

writeLongPropertyback to summary
pack-priv void writeLongProperty(String name, long value)

Write a property with a long value. If the value is outside the "interop" range of IEEE-754 double-precision floating point (64-bit) then it is written as a string.

writePropertyback to summary
pack-priv void writeProperty(String name, Object obj)

Write a property.

Parameters
name:String

the property name, null for an unnamed property

obj:Object

the value or null

writePropertyback to summary
pack-priv void writeProperty(Object obj)

Write an unnamed property.

jdk.internal.vm back to summary

private Class ThreadDumper.JsonWriter.Node

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
pack-priv int

Constructor Summary

AccessConstructor and Description
pack-priv
Node(boolean isArray)

Method Summary

Modifier and TypeMethod and Description
pack-priv int
pack-priv boolean
pack-priv int
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

isArrayback to summary
pack-priv final boolean isArray
propertyCountback to summary
pack-priv int propertyCount

Constructor Detail

Nodeback to summary
pack-priv Node(boolean isArray)

Method Detail

getAndIncrementPropertyCountback to summary
pack-priv int getAndIncrementPropertyCount()
isArrayback to summary
pack-priv boolean isArray()
propertyCountback to summary
pack-priv int propertyCount()
jdk.internal.vm back to summary

private Class ThreadDumper.TextWriter

extends Writer
Class Inheritance

Simple Writer implementation for printing text. The print/println methods throw UncheckedIOException if an I/O error occurs.

Field Summary

Modifier and TypeField and Description
private final Writer
Inherited from java.io.Writer:
lock

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Closes the stream, flushing it first.

public void
flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flushes the stream.

pack-priv void
pack-priv void
pack-priv void
pack-priv void
public void
write(char[] cbuf, int off, int len)

Implements abstract java.io.Writer.write.

Writes a portion of an array of characters.

Inherited from java.io.Writer:
appendappendappendnullWriterwritewritewritewrite

Field Detail

delegateback to summary
private final Writer delegate

Constructor Detail

TextWriterback to summary
pack-priv TextWriter(OutputStream out)

Method Detail

closeback to summary
public void close() throws IOException

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Doc from java.io.Writer.close.

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
flushback to summary
public void flush() throws IOException

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Doc from java.io.Writer.flush.

Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
printback to summary
pack-priv void print(Object obj)
printlnback to summary
pack-priv void println()
printlnback to summary
pack-priv void println(String s)
printlnback to summary
pack-priv void println(Object obj)
writeback to summary
public void write(char[] cbuf, int off, int len) throws IOException

Implements abstract java.io.Writer.write.

Doc from java.io.Writer.write.

Writes a portion of an array of characters.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs