线程在Android的 [英] Threading in Android

查看:102
本文介绍了线程在Android的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发的Andr​​oid应用程序,它需要从互联网上下载的内容。我使用的线程来做到这一点,然后调用runOnUiThread方法来更新GUI。

  1. 在我把它刷新菜单,如果用户尝试刷新内容,下载线程将被创建和启动。问题是,我怎么能控制线程顺序,我需要接受最新的请求的响应,并放弃previous线程的请求,如果有一些其他的请求仍在运行,因为请求参数可能已被用户更改。目前我使用的是threadId的做这件事,当一个线程完成后,它会检查其threadId的,如果它是最新的条记录之一,然后把控制和呈现的响应。 我的问题是那里呢?
  2. 任何其他合适的更好的解决方案
  3. 我是否需要停止线程,当用户退出应用程序?我记得,有些书说,不要试图手动停止线程,并等待本身完成是一个很好的做法,是真的吗?我应该通过调用停止或中断的方法阻止他们?
  4. 在我读线程周围的一些文件在Android和发现的类 HandlerThread ,是什么呢?在什么样的情况,我需要使用它吗?
解决方案

而不是开始一个新的线程,每刷新操作我将会为所有的后台下载的工作,循环和下载内容在队列中排队单线程。这可以确保你不下载内容的同时,也节约了资源。

在GUI中,您只需排队每当用户提示您刷新请求,并可以通过调用<一个中止运行下载href="http://developer.android.com/reference/org/apache/http/client/methods/Htt$p$pquestBase.html#abort%28%29"相对=nofollow> 的Htt prequestBase.abort 上的HTTP方法实例。后台线程应接收并搭上 SocketException 并移动到下一个排队的请求。

要结束后台线程你就必须结束其循环。您可以使用 尺蠖 处理程序 类来帮助你与所有上述情况,你提到的 HandlerThread 类只是一个方便的类来创建一个线程,拥有的 尺蠖

中断问题荷兰国际集团一个线程,它不会破坏你出一个阻塞I / O请求和处理的 InterruptException 正确可能会很复杂。所以,根据我会说是的情况下,更好的做法是由它的运行方法。

返回结束线程

I am currently developing Android app, it needs download content from internet. I use thread to do that and then call runOnUiThread method to update GUI.

  1. I placed a refresh menu on it, if user tried to refresh the content, the download thread will be created and started. The problem is that how can I control the thread order, I need to accept the latest request's response and abandon previous thread requests if there were some other requests still running because the request parameters may have been changed by user. Currently I was using a threadId to do this thing, when a thread finished, it will check its threadId, if it was the latest recored one, it then takes control and render the response. My question is that is there any other proper better solution for this?
  2. Do I need to stop threads when user exit the app? I remember that some book said that do not try stop thread manually and wait itself finish is a good practice, is that true? Should I stop them by calling "stop" or "interrupt" method?
  3. I read some documents around threading in Android and found the class HandlerThread, what is it? In what kind of situation I need to use it?

解决方案

Rather than starting a new thread for every refresh action I would create a single thread for all the background download work that loops and downloads content as lined up in a queue. That ensures that you don't download content concurrently and also saves resources.

In the GUI you simply queue a refresh request whenever the user prompts you to and can abort a running download by calling HttpRequestBase.abort on the http method instance. The background thread should receive and catch a SocketException and move on to the next queued request.

To end the background thread you just have to end its loop. You can use the Looper and Handler classes to help you with all of the above, the HandlerThread class you mentioned is simply a handy class to create a thread that has a Looper.

The problem with interrupting a thread is that it won't break you out of a blocking I/O request and handling an InterruptException correctly can be complicated. So depending on the situation I would say yes, it is better practice to end the thread by returning from its run method.

这篇关于线程在Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆