Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Using ThreadMXBean to Understand Threads

Use this interface to learn more about the threads in your code.


advertisement

The java.lang.management.ThreadMXBean is an interface that helps us to understand a lot about Threads in our code. There are numerous methods that participate to provide excellent visibility of the Threads.

Something that caught me by surprise was the method findDeadlockedThreads() that returns a list of all Threads that are in dead-lock at the moment. Ideally, the method returns an array of Thread IDs that we can use and effectively program in our subsequent process.

Here is some sample code:

ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); //Acquiring an instance
//Array with Thread id's that are deadlocked. This will return null incase there are no deadlocked Threads.
long[] threadIds = threadMXBean.findDeadlockedThreads();

You can prepare your own monitoring class and include the lines above.

A list of other methods in this interface can be found here.

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date