Java Android Studio 线程调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29245411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio threaded debugging
提问by miguel
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.
我在使用 Android Studio 1.1 调试多线程应用程序时遇到问题。似乎当遇到断点时,所有其他线程也会停止,而不仅仅是带有断点的线程。我在 Activity 的 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.
我在线程 a 和线程 b 的 Log.v 行设置断点,然后在 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().
当应用程序启动时,它会遇到线程 a 中的断点,但我注意到的第一个问题是应用程序的 UI 是空白的,就像主线程暂停一样。接下来,我看到 thread-b 中的断点也被命中,所以我在 Android Studio 的调试器中拉出 Threads 视图,但是当我去展开 thread-b 箭头时,那里什么也没有。当我展开主线程时,它显示它在 onStart() 的某处暂停。
Am I doing something wrong or is this debugger incapable of debugging multiple threads at once?
我做错了什么还是这个调试器不能同时调试多个线程?
采纳答案by Mister Smith
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.
在 IntelliJ IDEA(并且 Android Studio 基于 IntelliJ)中,当您放置断点时,如果您右键单击它,则会显示一个对话框,您可以选择是暂停所有线程(默认)还是仅暂停该线程。
You are pausing all the threads as it is the default setting.
您正在暂停所有线程,因为它是默认设置。
回答by CJBS
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).
在 Android Studio 中,可以指定给定的断点是暂停正在执行的线程(触发断点)还是所有线程的执行。这是基于每个断点的(即一些断点可以挂起所有线程,而其他断点仅挂起当前线程)。
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:
如果只有当前线程被挂起(上图中的选项“线程”),则其他未挂起线程的堆栈帧将在 Frames 窗口中不可见: