Three ways to enable threads in Android

Keywords: Android Programming

Three ways to enable threads in Android

In multithreaded programming, we often use three classes: handler, thread and Runnable. Do you know the relationship between them?

First of all Android The minimum unit of CPU allocation of is Thread, and Handler is generally created in a Thread, so Handler and Thread are bound to each other, one-to-one correspondence.  
Runnable is an interface and Thread is a subclass of runnable. So both of them are a process.  
HandlerThread, as the name implies, is a thread that can handle message loops. It is a thread with Looper and can handle message loops.

Rather than binding a Handler to a thread, the Handler corresponds to the Looper one by one.

Handler is the bridge between Activity and Thread/runnable. While the handler runs in the main UI thread, it and its sub threads can pass data through the Message object

1. The first way to enable is to implement a Thread by inheriting the Thread class and overriding the run method

public class MyThread extends Thread {  

    //Inherit Thread class and override its run method        
    private final static String TAG = "My Thread ===> ";      
    public void run(){  
        Log.d(TAG, "run");  
        for(int i = 0; i<100; i++)  
        {  
            Log.e(TAG, Thread.currentThread().getName() + "i =  " + i);  
        }  
    }  
} 

 

start-up

new MyThread().start();  
  • 1
  • 1

2. The second enabling method is to create a Runnable object

public class MyRunnable implements Runnable{  
    private final static String TAG = "My Runnable ===> ";  

    @Override  
    public void run() {  
        // TODO Auto-generated method stub  
        Log.d(TAG, "run");  
        for(int i = 0; i<1000; i++)  
        {  
            Log.e(TAG, Thread.currentThread().getName() + "i =  " + i);  
        }  
    }  
} 

 

start-up

  new Thread(new MyRunnable()).start(); 

 

Another way to enable

 btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                           ...
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

3. The third way is to start the thread through the Handler

public class MainActivity extends Activity {      
    private final static String TAG = "UOfly Android Thread ==>";  
    private int count = 0;  
    private Handler mHandler = new Handler();  
    private Runnable mRunnable = new Runnable() {  
        public void run() {  
            Log.e(TAG, Thread.currentThread().getName() + " " + count);  
            count++;  
            setTitle("" + count);  
            // Every 3 seconds  
            mHandler.postDelayed(mRunnable, 3000);  //Send messages to yourself, self run
        }  
    };  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        // Start thread through Handler  
        mHandler.post(mRunnable);  //Send message, start thread running
    }  

      @Override      
         protected void onDestroy() {       
             //Destroy the thread       
             mHandler.removeCallbacks(mRunnable);       
             super.onDestroy();       
         }       

} 

Posted by access9 on Fri, 27 Mar 2020 08:00:45 -0700