Android Studio threaded debugging

2019-02-01 17:32发布

问题:

I've been having trouble debugging a multithreaded app with Android Studio 1.1. It seems as if when a breakpoint is hit all other threads also stop, not just the one with the breakpoint. I created a simple test app with the following method in the Activity's onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Thread a = new Thread("thread-a") {
        @Override
        public void run() {
            Log.v("thread", "thread-a");
        }
    };

    Thread b = new Thread("thread-b") {
        @Override
        public void run() {
            Log.v("thread", "thread-b");
        }
    };

    a.start();
    b.start();
}

I set breakpoints at the Log.v lines in thread-a and thread-b and then I run it in debug mode on my Lollipop Nexus 5.

When the app starts it hits the breakpoint in thread-a but the first problem I notice is that the app's UI is blank as if the main thread is paused. Next I went to see that the breakpoint in thread-b is also hit so I pull up the Threads view in Android Studio's debugger but when I go to expand the thread-b arrow there's nothing there. When I expand the main thread it shows it is paused somewhere in onStart().

Am I doing something wrong or is this debugger incapable of debugging multiple threads at once?

回答1:

In IntelliJ IDEA (and Android Studio is based on IntelliJ), when you place a breakpoint, if you do right click over it a dialog is displayed and you can select whether to pause all threads (the default) or only that thread.

You are pausing all the threads as it is the default setting.



回答2:

In Android Studio, it's possible to specify whether a given breakpoint will suspend execution of just the executing thread (that triggers the breakpoint) or all threads. This is on a per-breakpoint basis (i.e. some breakpoints can suspend all threads, while others only suspend the current thread).

Right-click on a breakpoint to bring up the Breakpoint Properties window:

Note the 'Make Default' option which allows this to be defaulted for all newly created breakpoints.

If only the current thread is suspended (option "Thread" in the image above), then the stack-frame of other unsuspended threads will not be visible in the Frames window: