小言_互联网的博客

Service和Thread的区别

468人阅读  评论(0)

首先我们先来看看google对服务和线程的描述:

Services

A Service is an application component that can perform long-running operations in the background, and it doesn’t provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

If you must perform work outside of your main thread, but only while the user is interacting with your application, you should instead create a new thread. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), and stop it in onStop().

服务是可以在后台执行长时间运行的操作的应用程序组件,并且不提供用户界面。另一个应用程序组件可以启动服务,并且即使用户切换到另一个应用程序,它也可以在后台继续运行。此外,组件可以绑定到服务以与其进行交互,甚至可以执行进程间通信(IPC)。例如,一项服务可以从后台处理网络事务,播放音乐,执行文件I / O或与内容提供者进行交互。

如果必须在主线程之外执行工作,而仅在用户与应用程序交互时执行,则应该创建一个新线程。例如,如果您想播放一些音乐,但仅在活动运行时播放,则可以在onCreate()中创建一个线程,在onStart()中开始运行它,然后在onStop()中停止它。

Threads

When an application is launched, the system creates a thread of execution for the application, called “main.” This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. It is also almost always the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread. However, under special circumstances, an app’s main thread might not be its UI thread.

When your app performs intensive work in response to user interaction, this single thread model can yield poor performance unless you implement your application properly. Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user’s perspective, the application appears to hang. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous “application not responding” (ANR) dialog. The user might then decide to quit your application and uninstall it if they are unhappy.

启动应用程序时,系统会为该应用程序创建一个执行线程,称为“主线程”。该线程非常重要,因为它负责将事件调度到适当的用户界面小部件,包括绘图事件。应用程序几乎总是与Android UI工具包中的组件(android.widget和android.view包中的组件)进行交互的线程。这样,主线程有时也称为UI线程。但是,在特殊情况下,应用程序的主线程可能不是其UI线程。

当您的应用程序响应用户交互而执行大量工作时,除非您正确实施应用程序,否则此单线程模型可能会产生较差的性能。具体来说,如果UI线程中发生了所有事情,那么执行长时间的操作(如网络访问或数据库查询)将阻塞整个UI。当线程被阻塞时,不能调度任何事件,包括绘图事件。从用户的角度来看,该应用程序似乎已挂起。更糟糕的是,如果UI线程被阻塞了几秒钟(当前大约5秒钟),则会向用户显示臭名昭著的“应用程序无响应”(ANR)对话框。然后,如果用户不满意,他们可能决定退出您的应用程序并卸载它。

区别:

1.服务是Android的一种机制,主要是用来执行长时间运行在后台,不耗时和UI无关的操作,服务是执行在Main线程的,如果阻塞时间过长会出现ANR
线程 是程序执行的最小单元,可以用来执行耗时操作,阻塞时间过长不会也出现ANR

2.服务是四大组件之一需要进行注册,有生命周期,可以对执行的进程进行配置,只要获取到Context就可以对调用startService/bindService和stopService/unbindService进行控制,启动/绑定服务后,需要调用stopService/unbindService服务才会结束
线程没有生命周期,启动后只有对Thraed对象等业务执行完成进或者行手动终止才能结束,不灵活

小结

服务和线程完全不是同一个东西,都不参与UI操作,只是业务场景不一样,有时服务和线程都适用。

如果需要长时间运行在后台,不耗时和不操作UI可以考虑是使用服务,如播放音乐,下载服务等

如果需要做耗时操作,如网络请求,大量数据计算等,考虑是使用线程


转载:https://blog.csdn.net/Excellence1981/article/details/102484449
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场