SoFunction
Updated on 2025-04-09

Android programming custom thread pool and usage examples

This article describes the custom thread pool and usage of Android programming. Share it for your reference, as follows:

1. Overview:

1. Because the thread pool is fixed, the singleton mode is used.
2. Define two thread pools, long and short, used in different places respectively. Because singleton pattern is used, two are defined.
3. Define two methods, executed and cancelled

2. Code:

/**
  * @Description Thread Management Pool
  * @Project name App_Shop
  * @Bank Name
  * @Class Name ThreadManager
  * @author chenlin
  * @date March 29, 2014 10:17:06 pm
  */
public class ThreadManager {
  private ThreadManager(){};
  private static ThreadManager instance= new ThreadManager();
  private ThreadPoolProxy longPoolProxy;
  private ThreadPoolProxy shortPoolProxy;
  public static ThreadManager getInstance(){
    return instance;
  }
  /**
    * Long thread pool
    * @return
    */
  public synchronized ThreadPoolProxy createLongPool(){
    if (longPoolProxy == null) {
      //(int corePoolSize thread pool size, int maximumPoolSize maximum, long keepAliveTime survival time)      longPoolProxy = new ThreadPoolProxy(5, 5, 5000);
    }
    return longPoolProxy;
  }
  /**
    * Short thread pool
    * @return
    */
  public synchronized ThreadPoolProxy createShortPool(){
    if (shortPoolProxy == null) {
      shortPoolProxy = new ThreadPoolProxy(3, 3, 5000);
    }
    return shortPoolProxy;
  }
  public class ThreadPoolProxy{
    private ThreadPoolExecutor pool;
    private int corePoolSize; //Number of threads    private int maximumPoolSize; //The additional thread window will be opened after the thread is full    private long keepAliveTime;//Living time when no thread execution    public ThreadPoolProxy(int corePoolSize,int maximumPoolSize, long keepAliveTime){
       = corePoolSize;
       = maximumPoolSize;
       = keepAliveTime;
    }
    /**
      * Execution thread
      * @param runnable
      */
    public void execute(Runnable runnable){
      if (pool == null) {
        //How many threads can be queued up        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(10);
        pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, , workQueue);
      }
      (runnable);
    }
    /**
      * Cancel thread
      * @param runnable
      */
    public void cancel(Runnable runnable){
      if (pool != null) {
        (runnable);
      }
    }
  }
}

3. How to use:

().createLongPool().execute(new Runnable() {
  @Override
  public void run() {
    //(2000);
    final LoadResult result = loadFromServer();
    if (result != null) {
      (new Runnable() {
        @Override
        public void run() {
          currentState = ();
          showPages();
        }
      });
    }
  }
});

For more information about Android related content, please check out the topic of this site:Summary of the usage of Android threads and message mechanisms》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.