iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >在Android线程池里运行代码任务实例
  • 267
分享到

在Android线程池里运行代码任务实例

android线程池运行android线程程池Android 2022-06-06 10:06:45 267人浏览 独家记忆
摘要

本节展示如何在线程池里执行任务。流程是,添加一个任务到线程池的工作队列,当有线程可用时(执行完其他任务,空闲,或者还没执行任务),ThreadPoolExecutor会从队列里

本节展示如何在线程池里执行任务。流程是,添加一个任务到线程池的工作队列,当有线程可用时(执行完其他任务,空闲,或者还没执行任务),ThreadPoolExecutor会从队列里取任务,并在线程里运行。
本课同时向你展示了如何停止正在运行的任务。

在线程池里的线程上执行任务

在ThreadPoolExecutor.execute()里传入 Runnable对象启动任务。这个方法会把任务添加到线程池工作队列。当有空闲线程时,管理器会取出等待最久的任务,在线程上运行。
代码如下:
public class PhotoManager {
    public void handleState(PhotoTask photoTask, int state) {
        switch (state) {
            // The task finished downloading the image
            case DOWNLOAD_COMPLETE:
            // Decodes the image
                mDecodeThreadPool.execute(
                        photoTask.getPhotoDecodeRunnable());
            ...
        }
        ...
    }
    ...
}

当ThreadPoolExecutor启动Runnable时,会自动调用run()方法。

中断正在运行的代码

要停止任务,你需要中断任务的进程。你需要在创建任务的时候,保存一个当前线程的handle.
如:
代码如下:
class PhotoDecodeRunnable implements Runnable {
    // Defines the code to run for this task
    public void run() {
       
        mPhotoTask.setImageDecodeThread(Thread.currentThread());
        ...
    }
    ...
}

要中断线程,调用Thread.interrupt()就可以了。提示:线程对象是系统控制的,可以在你的app进程外被编辑。因为这个原因,你需要在中断它前加访问,放到一个同步块里:
代码如下:
public class PhotoManager {
    public static void cancelAll() {
       
        Runnable[] runnableArray = new Runnable[mDecodeWorkQueue.size()];
        // Populates the array with the Runnables in the queue
        mDecodeWorkQueue.toArray(runnableArray);
        // Stores the array length in order to iterate over the array
        int len = runnableArray.length;
       
        synchronized (sInstance) {
            // Iterates over the array of tasks
            for (int runnableIndex = 0; runnableIndex < len; runnableIndex++) {
                // Gets the current thread
                Thread thread = runnableArray[taskArrayIndex].mThread;
                // if the Thread exists, post an interrupt to it
                if (null != thread) {
                    thread.interrupt();
                }
            }
        }
    }
    ...
}

在大多数案例里,Thread.interrupt()会马上停止线程。可是,它只会停止在等待的线程,但不会中断cpu或network-intensive任务。为了避免系统变慢,你应该在开始尝试操作前测试等待中断的请求。

代码如下:

if (Thread.interrupted()) {
    return;
}
...
// Decodes a byte array into a Bitmap (CPU-intensive)
BitmapFactory.decodeByteArray(
        imageBuffer, 0, imageBuffer.length, bitmapOptions);
...

您可能感兴趣的文章:Android之线程池ThreadPoolExecutor的简介Android线程池控制并发多线程下载完全解析Android多线程中线程池ThreadPool的原理和使用浅谈Android中线程池的管理Android编程自定义线程池与用法示例浅谈Android 的线程和线程池的使用Android自带的四种线程池使用总结Android开发经验谈:并发编程(线程与线程池)(推荐)


--结束END--

本文标题: 在Android线程池里运行代码任务实例

本文链接: https://www.lsjlt.com/news/27038.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作