The relationship between Service and Thread

 

The relationship between Service and Thread

Many Android beginners may have such doubts, what is the relationship between Service and Thread? When should I use Service and when should I use Thread? The answer may surprise you a bit, because there is no relationship between Service and Thread!

The reason why many people associate them is mainly because of the background concept of Service. Thread, as we all know, is used to start a sub-thread, and performing some time-consuming operations here will not block the running of the main thread. When we first understand Service, we always think that it is used to process some background tasks, and some time-consuming operations can also be run here, which will cause confusion. However, if I tell you that Service actually runs in the main thread, do you still think it has anything to do with Thread? Let's look at this hard truth.

Add a line to print the current thread id in the onCreate() method of MainActivity:

Log.d("MyService", "MainActivity thread id is " + Thread.currentThread().getId());

 Then add a line to print the current thread id in the onCreate() method of MyService:

Log.d("MyService", "MyService thread id is " + Thread.currentThread().getId());  

 Now re-run the program and click the Start Service button, you will see the following print log:

     

It can be seen that their thread IDs are exactly the same, which confirms that the Service is indeed running in the main thread, that is to say, if you write very time-consuming code in the Service, the program will definitely appear ANR.

You may exclaim, this is not a fool! ? Then what's the use of me wanting Service? In fact, don't associate the background and child threads together, these are two completely different concepts. Android's background means that its operation is completely independent of the UI. Even if the Activity is destroyed, or the program is closed, the Service can continue to run as long as the process is still there. For example, some applications, which always need to maintain a heartbeat connection with the server, can use Service to achieve this. You may ask again, didn't you just verify that the Service is running in the main thread? The heartbeat connection has been executed here, won't it block the main thread? Of course, but we can create another sub-thread in the Service, and then deal with the time-consuming logic here.

Well, since a child thread is also created in the Service, why not create it directly in the Activity? This is because it is difficult for Activity to control Thread. When Activity is destroyed, there is no other way to re-acquire the instance of the previously created child thread. And the child thread created in one Activity, another Activity cannot operate on it. But Service is different. All Activity can be associated with Service, and then the methods can be easily operated. Even if Activity is destroyed, as long as it is re-associated with Service, the original Service can be obtained again. An instance of Binder. Therefore, using Service to handle background tasks, Activity can be finished with confidence, and there is no need to worry about the situation that background tasks cannot be controlled.

A more standard Service can be written as:

@Override  
public int onStartCommand(Intent intent, int flags, int startId) {  
    new Thread(new Runnable() {  
        @Override  
        public void run() {  
            // start executing background tasks  
        }  
    }).start();  
    return super.onStartCommand(intent, flags, startId);  
}  
  
class MyBinder extends Binder {  
  
    public void startDownload() {  
        new Thread(new Runnable() {  
            @Override  
            public void run() {  
                // 执行具体的下载任务  
            }  
        }).start();  
    }  
  
}  

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326747002&siteId=291194637