Android Open Source - testApp1_android Async Task Manager






From Project

Back to project page testApp1_android.

License

The source code is released under:

MIT License

If you think the Android project testApp1_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package h.d.t.controller;
//from w  ww  . j  ava  2s.  c o m
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.content.Context;

/**
 * H? tr? vi?c x? l cc thao tc c th??i gian x? l di (th???ng l trn 200ms)
 * nh? load d? li?u t? server v??, nguyn nhn th???ng do vi?c t?i cc d? li?u t?
 * server v?? ho?c parse cc d? li?u t? cache m?t kh nhi??u th??i gian, n?u th?c
 * hi?n trong main thread s? lm block ui, ng??c l?i n?u cc mn hnh khc nhau
 * ???u t? t?o v qu?n l thread m?t cch ??c l?p th s? kh ki?m sot s? l??ng
 * thread c?a ton b? ?ng d?ng, c th? s? gy ?nh h??ng c? v?? memory, network
 * resource...
 * 
 * V?i AsyncTaskManager ny, m??i request g?i t?i ???u ???c ??a vo thread pool ??
 * x? l (xem {@link #mTaskThreadPool}f), s? l??ng thread s? ???c gi?i h?n ? m?c
 * ?? ph h?p, bn c?nh ? c m?t thread v?i ?? ?u tin cao dnh c cc request
 * yu c?u ???c th?c thi g?p, n?u khng th UI s? ph?i ch?? (xem
 * {@link #PRIORITY_BLOCKING})
 * 
 * @author Tran Vu Tat Binh (tranvutatbinh@gmail.com)
 * 
 */
public class AsyncTaskManager {
  /**
   * ??? ?u tin bnh th???ng, dnh cho cc task m UI khng b? block khi ch??
   * k?t qu?
   */
  private static final Object mLock = new Object();

  // Singleton
  private static AsyncTaskManager mInstance;

  /**
   * S? l??ng thread trong thread pool
   */
  private static final int CORE_NORMAL_POOL_SIZE = 0;

  /**
   * S? l??ng thread t?i ?a trong thread pool, hi?n t?i ?? b?ng s? l??ng trong
   * tr???ng h?p bnh th???ng ?? gi?i h?n t?i m?c ? lun
   */
  private static final int MAXIMUM_NORMAL_POOL_SIZE = 1;

  /**
   * Th??i gian gi? m?t thread t?n t?i ?? ch?? dng l?i sau khi th?c thi xong
   */
  private static final int KEEP_ALIVE_TIME = 2;

  /**
   * Hng ??i cc task c?n th?c thi v?i thread pool
   */
  private final BlockingQueue<Runnable> mNormalTaskQueue;

  /**
   * Thread pool ?? x? l cc task thng th???ng khng ?i h??i ?? ?u tin cao
   */
  private final ThreadPoolExecutor mTaskThreadPool;

  public static AsyncTaskManager getInstance(Context context) {
    synchronized (mLock) {
      if (mInstance == null) {
        mInstance = new AsyncTaskManager(
            context.getApplicationContext());
      }
      return mInstance;
    }
  }

  private AsyncTaskManager(Context context) {

    // Nh?ng task background c ?? ?u tin trung bnh
    mNormalTaskQueue = new LinkedBlockingQueue<Runnable>();
    mTaskThreadPool = new ThreadPoolExecutor(CORE_NORMAL_POOL_SIZE,
        MAXIMUM_NORMAL_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
        mNormalTaskQueue);
    mTaskThreadPool.allowCoreThreadTimeOut(true);

  }

  public void execute(Runnable runnable) {
    mTaskThreadPool.execute(runnable);
  }

  /**
   * Hon th?c thi m?t task no ?
   */
  public void cancel(Runnable runnable) {
    mTaskThreadPool.remove(runnable);
  }

}




Java Source Code List

h.d.t.controller.AsyncTaskManager.java
h.d.t.data.KaraokeDB.java
h.d.t.dkaraoke.DKaraoke.java
h.d.t.model.Song.java