2

I have a Thread in my Android code and while debugging, I need to set a breakpoint inside the thread. However, once I am at the breakpoint inside the thread, I can't see the stacktrace of the calling thread. At this point, I usually need to set a new breakpoint at the calling thread and take it from there.

enter image description here

As you can see in the above image, the stack trace in the debugger shows run() as the top-level method. How can I move up in the stack trace, or switch threads to see start() and whatever came before it?

Community
  • 1
  • 1
The Hungry Androider
  • 2,158
  • 5
  • 24
  • 45

1 Answers1

2

You cannot or shouldn't see the stacktrace of the first thread that created another thread. The way it works in multithreading is each will be working in parallel. What could happen is the first thread calls the creation of the second thread and then continues to execute other methods or work, while the OS takes care of creating the second thread and starting at certain time (usually ASAP) to do some execution as well. The second thread will be containing an almost stacktrace as the first method it is executing is run. It'll fill as the code accesses other methods and empties as it exits/finishes the methods it was executing. Hence a code like this:

Log.i("FirstThread", "creating secondThread");
Thread secondThread = new Thread(new Runnable() {
    @Override
    public void run() {
        Log.i("SecondThread", "run called");
    }
});
secondThread.start();
Log.i("FirstThread", "finished creating secondThread");

might end up with the following log:

FirstThread: creating secondThread
FirstThread: finished creating secondThread
SecondThread: run called

Bottom line is the first log will always be the first since the thread second thread is created after it, but the second and the third logs could switch at different times and trials (one comes before the other and vice versa). That's because they are working in parallel and the OS is managing the time the second thread starts and the resources for it do the execution.

You could see what the other thread is executing, however it could be the main UI thread of Android that you'll be seeing, so it could be not executing your code and be executing other things related to drawing your UI and system related stuff, hence to keep your application working.

To do so, just right click on the break point that you've placed within the thread, enable Suspend and choose All as the image below, then press Done:

enter image description here

ahasbini
  • 6,156
  • 1
  • 24
  • 40