APIdia Launch Partner
Top Description Methods
com.sun.management

public Interface ThreadMXBean

extends ThreadMXBean
Known Direct Implementers
com.sun.management.internal.HotSpotThreadImpl
Imports
java.util.Map

Platform-specific management interface for the thread system of the Java virtual machine.

This platform extension is only available to a thread implementation that supports this extension.

Author
Paul Hohensee
Since
6u25

Method Summary

Modifier and TypeMethod and Description
public default long

Returns:

an approximation of the total memory allocated, in bytes, in heap memory for the current thread if thread memory allocation measurement is enabled; -1 otherwise.
getCurrentThreadAllocatedBytes
()

Returns an approximation of the total amount of memory, in bytes, allocated in heap memory for the current thread.

public long

Returns:

an approximation of the total memory allocated, in bytes, in heap memory for the thread with the specified ID if the thread with the specified ID is a platform thread, the thread is alive, and thread memory allocation measurement is enabled; -1 otherwise.
getThreadAllocatedBytes
(long
the thread ID of a thread
id
)

Returns an approximation of the total amount of memory, in bytes, allocated in heap memory for the thread with the specified ID. The returned value is an approximation because some Java virtual machine implementations may use object allocation mechanisms that result in a delay between the time an object is allocated and the time its size is recorded.

public long[]

Returns:

an array of long values, each of which is an approximation of the total memory allocated, in bytes, in heap memory for the thread whose ID is in the corresponding element of the input array of IDs.
getThreadAllocatedBytes
(long[]
an array of thread IDs.
ids
)

Returns an approximation of the total amount of memory, in bytes, allocated in heap memory for each thread whose ID is in the input array ids.

public long[]

Returns:

an array of long values, each of which is the amount of CPU time the thread whose ID is in the corresponding element of the input array of IDs has used, if the thread of a specified ID is a platform thread, the thread is alive, and CPU time measurement is enabled; -1 otherwise.
getThreadCpuTime
(long[]
an array of thread IDs.
ids
)

Returns the total CPU time for each thread whose ID is in the input array ids in nanoseconds.

public long[]

Returns:

an array of long values, each of which is the amount of user mode CPU time the thread whose ID is in the corresponding element of the input array of IDs has used, if the thread of a specified ID is a platform thread, the thread is alive, and CPU time measurement is enabled; -1 otherwise.
getThreadUserTime
(long[]
an array of thread IDs.
ids
)

Returns the CPU time that each thread whose ID is in the input array ids has executed in user mode in nanoseconds.

public default long

Returns:

an approximation of the total memory allocated, in bytes, in heap memory since the Java virtual machine was started, if thread memory allocation measurement is enabled; -1 otherwise.
getTotalThreadAllocatedBytes
()

Returns an approximation of the total amount of memory, in bytes, allocated in heap memory by all threads since the Java virtual machine started.

public boolean

Returns:

true if thread memory allocation measurement is enabled; false otherwise.
isThreadAllocatedMemoryEnabled
()

Tests if thread memory allocation measurement is enabled.

public boolean

Returns:

true if the Java virtual machine implementation supports thread memory allocation measurement; false otherwise.
isThreadAllocatedMemorySupported
()

Tests if the Java virtual machine implementation supports thread memory allocation measurement.

public void
setThreadAllocatedMemoryEnabled(boolean
true to enable; false to disable.
enable
)

Enables or disables thread memory allocation measurement.

Inherited from java.lang.management.ThreadMXBean:
dumpAllThreadsdumpAllThreadsfindDeadlockedThreadsfindMonitorDeadlockedThreadsgetAllThreadIdsgetCurrentThreadCpuTimegetCurrentThreadUserTimegetDaemonThreadCountgetPeakThreadCountgetThreadCountgetThreadCpuTimegetThreadInfogetThreadInfogetThreadInfogetThreadInfogetThreadInfogetThreadInfogetThreadUserTimegetTotalStartedThreadCountisCurrentThreadCpuTimeSupportedisObjectMonitorUsageSupportedisSynchronizerUsageSupportedisThreadContentionMonitoringEnabledisThreadContentionMonitoringSupportedisThreadCpuTimeEnabledisThreadCpuTimeSupportedresetPeakThreadCountsetThreadContentionMonitoringEnabledsetThreadCpuTimeEnabled

Method Detail

getCurrentThreadAllocatedBytesback to summary
public default long getCurrentThreadAllocatedBytes()

Returns an approximation of the total amount of memory, in bytes, allocated in heap memory for the current thread. The returned value is an approximation because some Java virtual machine implementations may use object allocation mechanisms that result in a delay between the time an object is allocated and the time its size is recorded.

This is a convenience method for local management use and is equivalent to calling:

  getThreadAllocatedBytes(Thread.currentThread().threadId());
Returns:long

an approximation of the total memory allocated, in bytes, in heap memory for the current thread if thread memory allocation measurement is enabled; -1 otherwise.

Exceptions
UnsupportedOperationException:
if the Java virtual machine implementation does not support thread memory allocation measurement.
Since
14
See Also
isThreadAllocatedMemorySupported, isThreadAllocatedMemoryEnabled, setThreadAllocatedMemoryEnabled
getThreadAllocatedBytesback to summary
public long getThreadAllocatedBytes(long id)

Returns an approximation of the total amount of memory, in bytes, allocated in heap memory for the thread with the specified ID. The returned value is an approximation because some Java virtual machine implementations may use object allocation mechanisms that result in a delay between the time an object is allocated and the time its size is recorded.

If the thread with the specified ID is a virtual thread, is not alive, or does not exist, this method returns -1. If thread memory allocation measurement is disabled, this method returns -1. A thread is alive if it has been started and has not yet terminated.

If thread memory allocation measurement is enabled after the thread has started, the Java virtual machine implementation may choose any time up to and including the time that the capability is enabled as the point where thread memory allocation measurement starts.

Parameters
id:long

the thread ID of a thread

Returns:long

an approximation of the total memory allocated, in bytes, in heap memory for the thread with the specified ID if the thread with the specified ID is a platform thread, the thread is alive, and thread memory allocation measurement is enabled; -1 otherwise.

Exceptions
IllegalArgumentException:
if id <= 0.
UnsupportedOperationException:
if the Java virtual machine implementation does not support thread memory allocation measurement.
See Also
isThreadAllocatedMemorySupported, isThreadAllocatedMemoryEnabled, setThreadAllocatedMemoryEnabled
getThreadAllocatedBytesback to summary
public long[] getThreadAllocatedBytes(long[] ids)

Returns an approximation of the total amount of memory, in bytes, allocated in heap memory for each thread whose ID is in the input array ids. The returned values are approximations because some Java virtual machine implementations may use object allocation mechanisms that result in a delay between the time an object is allocated and the time its size is recorded.

This method is equivalent to calling the getThreadAllocatedBytes(long) method for each thread ID in the input array ids and setting the returned value in the corresponding element of the returned array.

Parameters
ids:long[]

an array of thread IDs.

Returns:long[]

an array of long values, each of which is an approximation of the total memory allocated, in bytes, in heap memory for the thread whose ID is in the corresponding element of the input array of IDs.

Exceptions
NullPointerException:
if ids is null
IllegalArgumentException:
if any element in the input array ids is <= 0.
UnsupportedOperationException:
if the Java virtual machine implementation does not support thread memory allocation measurement.
See Also
getThreadAllocatedBytes(long), isThreadAllocatedMemorySupported, isThreadAllocatedMemoryEnabled, setThreadAllocatedMemoryEnabled
getThreadCpuTimeback to summary
public long[] getThreadCpuTime(long[] ids)

Returns the total CPU time for each thread whose ID is in the input array ids in nanoseconds. The returned values are of nanoseconds precision but not necessarily nanoseconds accuracy.

This method is equivalent to calling the ThreadMXBean#getThreadCpuTime(long) method for each thread ID in the input array ids and setting the returned value in the corresponding element of the returned array.

Parameters
ids:long[]

an array of thread IDs.

Returns:long[]

an array of long values, each of which is the amount of CPU time the thread whose ID is in the corresponding element of the input array of IDs has used, if the thread of a specified ID is a platform thread, the thread is alive, and CPU time measurement is enabled; -1 otherwise.

Exceptions
NullPointerException:
if ids is null
IllegalArgumentException:
if any element in the input array ids is <= 0.
UnsupportedOperationException:
if the Java virtual machine implementation does not support CPU time measurement.
See Also
ThreadMXBean#getThreadCpuTime(long), getThreadUserTime, ThreadMXBean#isThreadCpuTimeSupported, ThreadMXBean#isThreadCpuTimeEnabled, ThreadMXBean#setThreadCpuTimeEnabled
getThreadUserTimeback to summary
public long[] getThreadUserTime(long[] ids)

Returns the CPU time that each thread whose ID is in the input array ids has executed in user mode in nanoseconds. The returned values are of nanoseconds precision but not necessarily nanoseconds accuracy.

This method is equivalent to calling the ThreadMXBean#getThreadUserTime(long) method for each thread ID in the input array ids and setting the returned value in the corresponding element of the returned array.

Parameters
ids:long[]

an array of thread IDs.

Returns:long[]

an array of long values, each of which is the amount of user mode CPU time the thread whose ID is in the corresponding element of the input array of IDs has used, if the thread of a specified ID is a platform thread, the thread is alive, and CPU time measurement is enabled; -1 otherwise.

Exceptions
NullPointerException:
if ids is null
IllegalArgumentException:
if any element in the input array ids is <= 0.
UnsupportedOperationException:
if the Java virtual machine implementation does not support CPU time measurement.
See Also
ThreadMXBean#getThreadUserTime(long), getThreadCpuTime, ThreadMXBean#isThreadCpuTimeSupported, ThreadMXBean#isThreadCpuTimeEnabled, ThreadMXBean#setThreadCpuTimeEnabled
getTotalThreadAllocatedBytesback to summary
public default long getTotalThreadAllocatedBytes()

Returns an approximation of the total amount of memory, in bytes, allocated in heap memory by all threads since the Java virtual machine started. The returned value is an approximation because some Java virtual machine implementations may use object allocation mechanisms that result in a delay between the time an object is allocated and the time its size is recorded.

Implementation Specification

The default implementation throws UnsupportedOperationException if the Java virtual machine implementation does not support thread memory allocation measurement, and otherwise acts as though thread memory allocation measurement is disabled.

Returns:long

an approximation of the total memory allocated, in bytes, in heap memory since the Java virtual machine was started, if thread memory allocation measurement is enabled; -1 otherwise.

Exceptions
UnsupportedOperationException:
if the Java virtual machine implementation does not support thread memory allocation measurement.
Since
21
See Also
isThreadAllocatedMemorySupported, isThreadAllocatedMemoryEnabled, setThreadAllocatedMemoryEnabled
isThreadAllocatedMemoryEnabledback to summary
public boolean isThreadAllocatedMemoryEnabled()

Tests if thread memory allocation measurement is enabled.

Returns:boolean

true if thread memory allocation measurement is enabled; false otherwise.

Exceptions
UnsupportedOperationException:
if the Java virtual machine does not support thread memory allocation measurement.
See Also
isThreadAllocatedMemorySupported
isThreadAllocatedMemorySupportedback to summary
public boolean isThreadAllocatedMemorySupported()

Tests if the Java virtual machine implementation supports thread memory allocation measurement.

Returns:boolean

true if the Java virtual machine implementation supports thread memory allocation measurement; false otherwise.

setThreadAllocatedMemoryEnabledback to summary
public void setThreadAllocatedMemoryEnabled(boolean enable)

Enables or disables thread memory allocation measurement. The default is platform dependent.

Parameters
enable:boolean

true to enable; false to disable.

Exceptions
UnsupportedOperationException:
if the Java virtual machine does not support thread memory allocation measurement.
See Also
isThreadAllocatedMemorySupported