Skip to content

Commit

Permalink
Test for the usage of ThreadMXBean
Browse files Browse the repository at this point in the history
  • Loading branch information
aaneja committed Nov 21, 2024
1 parent 8a47eb5 commit 6b2a941
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 19 deletions.
52 changes: 33 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
# Compiled class file
*.class
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

# Log file
*.log
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

# BlueJ files
*.ctxt
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

# Mobile Tools for Java (J2ME)
.mtj.tmp/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
### VS Code ###
.vscode/

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
### Mac OS ###
.DS_Store
36 changes: 36 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>mvn-test</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>
<arg>--add-exports</arg>
<arg>jdk.management/com.ibm.lang.management.internal=ALL-UNNAMED</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
</dependencies>
</project>
30 changes: 30 additions & 0 deletions src/main/java/BeanTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import com.ibm.lang.management.internal.ExtendedThreadMXBeanImpl;
import com.sun.management.ThreadMXBean;

import java.lang.management.ManagementFactory;

public class BeanTester
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
ibmThreadMxBean();
sunThreadMxBean();
}

private static void ibmThreadMxBean()
{
System.out.println("IBM ThreadMXBean");
com.ibm.lang.management.ThreadMXBean ibmThreadBean = ExtendedThreadMXBeanImpl.getInstance();
System.out.printf("isThreadAllocatedMemorySupported : %s%n", ibmThreadBean.isThreadAllocatedMemorySupported());
System.out.printf("getThreadAllocatedBytes : %d%n", ibmThreadBean.getThreadAllocatedBytes(Thread.currentThread().getId()));
}

private static void sunThreadMxBean()
{
System.out.println("Sun ThreadMXBean");
ThreadMXBean sunThreadBean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
System.out.printf("isThreadAllocatedMemorySupported : %s%n", sunThreadBean.isThreadAllocatedMemorySupported());
System.out.printf("getThreadAllocatedBytes : %d%n", sunThreadBean.getThreadAllocatedBytes(Thread.currentThread().getId()));
}
}

0 comments on commit 6b2a941

Please sign in to comment.