JVISUALVM monitoring __java for Java code test thread deadlock

Source: Internet
Author: User

Test thread deadlock, run the following code:

public class Test1 {
	private static Object O1 = new Object ();
	private static Object O2 = new Object ();
	public static void Main (string[] args) {for
		(int i = 0;i<100;i++) {
			new Thread (new Runnable () {
				@Override 
 public void Run () {
					synchronized (O1) {
						synchronized (O2) {
							System.out.println ("AM O1");
						}
				}
			}). Start ();
			New Thread (New Runnable () {
				@Override public
				void Run () {
					synchronized (O2) {
						synchronized (O1) { C19/>system.out.println ("Am O2"),}}}
			). Start ();}}
Code thread 1 first to apply for OBJ1, and then apply for OBJ2, threads 2 first to apply for OBJ2, and then apply for obj1. If there are more executions, a deadlock will occur, and we are still looking at the thread's monitoring console:
There are jvisualvm.exe tools in the JDK C:\Program files\java\jdk1.8.0_101\bin directory

Run the above code to view the thread, as shown in the following illustration:

Select a thread to view the dump, you can view the specific cause of the deadlock, you can use this tool to view those threads in the program deadlock, so as to better troubleshoot errors.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.