Book Image

Testing and securing android studio applications

Book Image

Testing and securing android studio applications

Overview of this book

Table of Contents (18 chapters)
Testing and Securing Android Studio Applications
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Threads


The Threads tab displays the list of threads that are a part of the selected process. Applications have one main thread, also called as the UI thread, which dispatches the events to the user interface (UI) widgets. To perform long operations, it is necessary to create new threads so that the main thread is not blocked. If the main thread gets blocked, the whole UI will also get blocked.

To illustrate the working of this tool, run the following example. In Android Studio, create a new basic project with a main layout and a main activity. Add a button to the main layout named, for example, Start New Thread. Create a new method to be executed when the button is clicked and add the following code in the method:

public void startNewThread(View v){
  new Thread(new Runnable() {
    public void run() {
      Thread.currentThread().setName("My example Thread");
      
      try{
        Thread.sleep(30000);
      } catch (InterruptedException e){ 
        e.printStackTrace(); 
        }
...