View deadlocks through ANR

Source: Internet
Author: User

 

Deadlock is usually hard to find. However, in androidwe can send anr.txt to traces.txt. In traces.txt, you can view the threads in wait. Of course, the deadlocked threads are also in it. This provides great convenience for searching for deadlocks.

Note: After the method is ANR, the following file data \ anr \ traces.txt will be generated on the mobile phone.

The following example is provided.

File 1

DeadLockTraceActivity. java File

Package com. gameloft. robin;

Import android. app. Activity;

Import android. OS. Bundle;

Import android. OS. Handler;

Public class DeadLockTraceActivity extends Activity {

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. deadlock_trace );

}

String tag = "robin ";

Public void onResume ()

{

Super. onResume ();

New ThreadDeadLockExample (). start ();

Handler handler = new Handler ();

Runnable run = new Runnable () {public void run () {forceWait ();}};

Handler. post (run );

}

Synchronized void forceWait ()

{

Try {

Wait (60000 );

} Catch (InterruptedException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

}

File 2

ThreadDeadLockExample. java File

Package com. gameloft. robin;

Import java. util. concurrent. locks. ReentrantLock;

Import android. util. Log;

Public class ThreadDeadLockExample {

Private final ReentrantLock lockA = new ReentrantLock (true );

Private final ReentrantLock lockB = new ReentrantLock (true );

String tag = "robin ";

Void start ()

{

Log. I (tag, "ThreadDeadLockExample start ");

New ThreadA ("ThreadA"). start ();

New ThreadB ("ThreadB"). start ();

}

Class ThreadA extends Thread

{

ThreadA (String name)

{

Super (name );

}

Public void run ()

{

Log. I (tag, "try to lock A" + "in ThreadA ");

LockA. lock ();

Try {

Thread. sleep (1000 );

} Catch (InterruptedException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

Log. I (tag, "lock A successful" + "in ThreadA ");

Log. I (tag, "try to lock B" + "in ThreadA ");

LockB. lock ();

Log. I (tag, "lock B successful" + "in ThreadA ");

LockB. unlock ();

Log. I (tag, "unlock B" + "in ThreadA ");

LockA. unlock ();

Log. I (tag, "unlock A" + "in ThreadA ");

}

}

Class ThreadB extends Thread

{

ThreadB (String name)

{

Super (name );

}

Public void run ()

{

Log. I (tag, "try to lock B" + "in ThreadB ");

LockB. lock ();

Try {

Thread. sleep (1000 );

} Catch (InterruptedException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

Log. I (tag, "lock B successful" + "in ThreadB ");

Log. I (tag, "try to lock A" + "in ThreadB ");

LockA. lock ();

Log. I (tag, "lock A successful" + "in ThreadB ");

LockA. unlock ();

Log. I (tag, "unlock A" + "in ThreadB ");

LockB. unlock ();

Log. I (tag, "unlock B" + "in ThreadB ");

}

}

}

Traces.txt file Summary

Dalvik threads:

"Main" prio = 5 tid = 1 TIMED_WAIT

| Group = "main" sCount = 1 dsCount = 0 s = N obj = 0x400208b8 self = 0xcdf0

| Shard id = 1732 nice = 0 sched = 0/0 MRM = unknown handle =-1345026000

At java. lang. Object. wait (Native Method)

-Waiting on <0x4a5ba4b0> (a com. gameloft. robin. DeadLockTraceActivity)

At java. lang. Object. wait (Object. java: 326)

At com. gameloft. robin. DeadLockTraceActivity. forceWait (DeadLockTraceActivity. java: 24)

At com. gameloft. robin. DeadLockTraceActivity $ 1.run( DeadLockTraceActivity. java: 18)

At android. OS. Handler. handleCallback (Handler. java: 587)

At android. OS. Handler. dispatchMessage (Handler. java: 92)

At android. OS. lorule. loop (lorule. java: 123)

At android. app. ActivityThread. main (ActivityThread. java: 4633)

At java. lang. reflect. Method. invokeNative (Native Method)

At java. lang. reflect. Method. invoke (Method. java: 521)

At com. android. internal. OS. ZygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 858)

At com. android. internal. OS. ZygoteInit. main (ZygoteInit. java: 616)

At dalvik. system. NativeStart. main (Native Method)

 

"ThreadB" prio = 5 tid = 10 WAIT

| Group = "main" sCount = 1 dsCount = 0 s = N obj = 0x4a5bf868 self = 0x2174f8

| Shard id = 1741 nice = 0 sched = 0/0 MRM = unknown handle = 2192952

At java. lang. Object. wait (Native Method)

-Waiting on <0x4a5bf980> (a java. lang. VMThread)

At java. lang. Thread. parkFor (Thread. java: 1535)

At java. lang. LangAccessImpl. parkFor (LangAccessImpl. java: 48)

At sun. misc. Unsafe. park (Unsafe. java: 317)

At java. util. concurrent. locks. LockSupport. park (LockSupport. java: 131)

At java. util. concurrent. locks. AbstractQueuedSynchronizer. parkAndCheckInterrupt (AbstractQueuedSynchronizer. java: 790)

At java. util. concurrent. locks. AbstractQueuedSynchronizer. acquireQueued (AbstractQueuedSynchronizer. java: 823)

At java. util. concurrent. locks. AbstractQueuedSynchronizer. acquire (AbstractQueuedSynchronizer. java: 1153)

At java. util. concurrent. locks. ReentrantLock $ FairSync. lock (ReentrantLock. java: 200)

At java. util. concurrent. locks. ReentrantLock. lock (ReentrantLock. java: 261)

At com. gameloft. robin. ThreadDeadLockExample $ ThreadB. run (ThreadDeadLockExample. java: 61)

 

"ThreadA" prio = 5 tid = 9 WAIT

| Group = "main" sCount = 1 dsCount = 0 s = N obj = 0x4a5bf438 self = 0x216858

| Shard id = 1740 nice = 0 sched = 0/0 MRM = unknown handle = 2189720

At java. lang. Object. wait (Native Method)

-Waiting on <0x4a5bf590> (a java. lang. VMThread)

At java. lang. Thread. parkFor (Thread. java: 1535)

At java. lang. LangAccessImpl. parkFor (LangAccessImpl. java: 48)

At sun. misc. Unsafe. park (Unsafe. java: 317)

At java. util. concurrent. locks. LockSupport. park (LockSupport. java: 131)

At java. util. concurrent. locks. AbstractQueuedSynchronizer. parkAndCheckInterrupt (AbstractQueuedSynchronizer. java: 790)

At java. util. concurrent. locks. AbstractQueuedSynchronizer. acquireQueued (AbstractQueuedSynchronizer. java: 823)

At java. util. concurrent. locks. AbstractQueuedSynchronizer. acquire (AbstractQueuedSynchronizer. java: 1153)

At java. util. concurrent. locks. ReentrantLock $ FairSync. lock (ReentrantLock. java: 200)

At java. util. concurrent. locks. ReentrantLock. lock (ReentrantLock. java: 261)

At com. gameloft. robin. ThreadDeadLockExample $ ThreadA. run (ThreadDeadLockExample. java: 35)

 

From the column of robin

Related Article

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.