Android 线程管理——摘自【极客学院】

2016-09-02  本文已影响48人  遇见_未见
  1. *** 使用私有构造方法***
    让构造方法私有从而保证这是一个单例,这意味着你不需要在同步代码块(synchronized block)中额外访问这个类:
    public class PhotoManager {
    ...
    * Constructs the work queues and thread pools used to download
    * and decode images. Because the constructor is marked private,
    * it's unavailable to other classes, even in the same package.
    */
    private PhotoManager() {
    ...
    }

  2. 通过调用线程池类里的方法开启你的任务
    在线程池类中定义一个能添加任务到线程池队列的方法。例如:
    public class PhotoManager {
    ...
    // Called by the PhotoView to get a photo
    static public PhotoTask startDownload(
    PhotoView imageView,
    boolean cacheFlag) {
    ...
    // Adds a download task to the thread pool for execution
    sInstance.
    mDownloadThreadPool.
    execute(downloadTask.getHTTPDownloadRunnable());
    ...
    }

  3. 在构造方法中实例化一个Handler,且将它附加到你APP的UI线程。
    一个Handler允许你的APP安全地调用UI对象(例如 View对象)的方法。大多数UI对象只能从UI线程安全的代码中被修改。这个方法将会在与UI线程进行通信(Communicate with the UI Thread)这一课中进行详细的描述。例如:
    private PhotoManager() {
    ...
    // Defines a Handler object that's attached to the UI thread
    mHandler = new Handler(Looper.getMainLooper()) {
    /*
    * handleMessage() defines the operations to perform when
    * the Handler receives a new Message to process.
    */
    @Override
    public void handleMessage(Message inputMessage) {
    ...
    }
    ...
    }
    }

  1. 线程保持活动状态的持续时间和时间单位
    这个是指线程被关闭前保持空闲状态的持续时间。这个持续时间通过时间单位值进行解译,是TimeUnit()中定义的常量之一。
  2. 一个任务队列
    这个传入的队列由ThreadPoolExecutor获取的Runnable对象组成。为了执行一个线程中的代码,一个线程池管理者从先进先出的队列中取出一个Runnable对象且把它附加到一个线程。当你创建线程池时需要提供一个队列对象,这个队列对象类必须实现BlockingQueue接口。为了满足你的APP的需求,你可以选择一个Android SDK中已经存在的队列实现类。为了学习更多相关的知识,你可以看一下ThreadPoolExecutor类的概述。下面是一个使用LinkedBlockingQueue实现的例子:
    public class PhotoManager {
    ...
    private PhotoManager() {
    ...
    // A queue of Runnables
    private final BlockingQueue<Runnable> mDecodeWorkQueue;
    ...
    // Instantiates the queue of Runnables as a LinkedBlockingQueue
    mDecodeWorkQueue = new LinkedBlockingQueue<Runnable>();
    ...
    }
    ...
    }
    创建一个线程池
    为了创建一个线程池,可以通过调用[ThreadPoolExecutor()](http://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.html#ThreadPoolExecutor(int, int, long, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue<java.lang.Runnable>))构造方法初始化一个线程池管理者对象,这样就能创建和管理一组可约束的线程了。如果线程池的初始化大小和最大大小相同,ThreadPoolExecutor在实例化的时候就会创建所有的线程对象。例如:
    private PhotoManager() {
    ...
    // Sets the amount of time an idle thread waits before terminating
    private static final int KEEP_ALIVE_TIME = 1;
    // Sets the Time Unit to seconds
    private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
    // Creates a thread pool manager
    mDecodeThreadPool = new ThreadPoolExecutor(
    NUMBER_OF_CORES, // Initial pool size
    NUMBER_OF_CORES, // Max pool size
    KEEP_ALIVE_TIME,
    KEEP_ALIVE_TIME_UNIT,
    mDecodeWorkQueue);
    }
上一篇 下一篇

猜你喜欢

热点阅读