Android asynchronous learning (1): Thread and Runnable

This article begins to learn Android asynchronous.

There are three main ways to implement asynchronous in Android:

  • Java Thread、Runnable
  • Android Handler、AsyncTask
  • RxAndroid

Android asynchronous learning (1): Thread and Runnable
Android asynchronous learning (2): Handler and AsyncTask
Android asynchronous learning (3): RxAndroid

This article uses Java's Thread and Runnable to achieve asynchronous

What is the relationship between Thread and Runnable?

In fact, Thread implements the Runnable interface in the source code.

Here is how they are used.

Thread

Relatively speaking, Thread is relatively simple to use

class DownThread extends Thread {
    
    
        @Override
        public void run() {
    
    
            super.run();
            // TODO执行操作
        }
    }
// 启动
new DownThread().start();

Runnable

Compared with Thread, use Runnable. To implement the Runnable interface, rewrite the run method and perform time-consuming operations.

new Thread(new Runnable() {
    
    
    @Override
    public void run() {
    
    
    	// TODO执行耗时操作
    }
}).start();

The boundaries between them are not so clear, and they are usually used together.

The above is the use of Thread and Runnable.

The next part is the learning of Handler, AsyncTask and RxAndroid.

Guess you like

Origin blog.csdn.net/A_Intelligence/article/details/109383094