Improve performance by using Jprofile to study thread allocation and synchronization to uncover deadlock and data contention issues

Source: Internet
Author: User

Deadlock Example program: ThreadTest, which is a classic example of a deadlock that occurs with synchronous nesting;

 Public classThreadTest { Public StaticObject Lock1 =NewObject ();  Public StaticObject Lock2 =NewObject ();  Public Static voidmethod1 () {Trace ("Inside method1"); synchronized(lock1) {Trace ("Method1 got Lock1");            DoSomething (); synchronized(Lock2) {Trace ("Method1 got Lock2, sleeping");                DoSomething (); Trace ("Method1 Returning"); }        }    }     Public Static voidmethod2 () {Trace ("Inside method2"); synchronized(Lock2) {Trace ("Method2 got Lock2");            DoSomething (); synchronized(lock1) {Trace ("Method2 got Lock1, sleeping");                DoSomething (); Trace ("Method2 Returning"); }        }    }     Public Static voiddosomething () {Try{Thread.Sleep (5*1000); }        Catch(Interruptedexception x) {x.printstacktrace (); } Trace ("Waiking Up"); }     Public Static voidTrace (String msg) {System.out.println (Thread.CurrentThread ()+ " : " +msg); }     Public Static voidMain (string[] args)throwsException {NewThread (NewRunnable () { Public voidrun () {method1 ();        }}). Start (); NewThread (NewRunnable () { Public voidrun () {method2 ();    }}). Start (); }}
View Code

Run:

You can see that two threads have been blocked;

To view the current lock view:

Right-click Export:

You can see the text description of the thread blocking situation;

Deadlock unlock cheats are to obtain the order of the locks to remain consistent;

Improve performance by using Jprofile to study thread allocation and synchronization to uncover deadlock and data contention issues

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.