Android使用信号量Semaphore进行多线程任务调度

话不多说,先上代码

  1. import android.os.Handler;  
  2. import android.os.Looper;  
  3. import android.os.Message;  
  4.   
  5. import java.util.LinkedList;  
  6. import java.util.concurrent.ExecutorService;  
  7. import java.util.concurrent.Executors;  
  8. import java.util.concurrent.Semaphore;  
  9.   
  10. public class TaskDispatcher {  
  11.   
  12.     private LinkedList<Runnable> mTaskList;         // 任务队列  
  13.     private ExecutorService mThreadPool;            // 线程池  
  14.     private Thread mPollingThead;                   // 轮询线程  
  15.     private Handler mPollingHanler;                 // 轮询线程中的Handler  
  16.     private static int mThreadCount = 3;            // 线程池的线程数量,默认为3  
  17.     private Type mType = Type.LIFO;                 // 队列的调度方式,默认为LIFO  
  18.     private volatile Semaphore mPollingSemaphore;   // 信号量,由于线程池内部也有一个阻塞线程,若加入任务的速度过快,LIFO效果不明显  
  19.     private volatile Semaphore mSemaphore = new Semaphore(0);  //信号量,防止mPoolThreadHander未初始化完成  
  20.   
  21.     private static TaskDispatcher mInstance;  
  22.   
  23.     public enum Type { FIFO, LIFO }  
  24.   
  25.     /** 
  26.      * 单例获得实例对象 
  27.      * @return   实例对象 
  28.      */  
  29.     public static TaskDispatcher getInstance() {  
  30.         if (mInstance == null) {  
  31.             synchronized (TaskDispatcher.class) {  
  32.                 if (mInstance == null) {  
  33.                     mInstance = new TaskDispatcher(mThreadCount, Type.LIFO);  
  34.                 }  
  35.             }  
  36.         }  
  37.         return mInstance;  
  38.     }  
  39.   
  40.     /** 
  41.      * 单例获得实例对象 
  42.      * @param threadCount    线程池的线程数量 
  43.      * @param type           队列的调度方式 
  44.      * @return   实例对象 
  45.      */  
  46.     public static TaskDispatcher getInstance(int threadCount, Type type) {  
  47.         if (mInstance == null) {  
  48.             synchronized (TaskDispatcher.class) {  
  49.                 if (mInstance == null) {  
  50.                     mInstance = new TaskDispatcher(threadCount, type);  
  51.                 }  
  52.             }  
  53.         }  
  54.         return mInstance;  
  55.     }  
  56.   
  57.     /** 
  58.      * 构造函数 
  59.      * @param threadCount    线程池的线程数量 
  60.      * @param type           队列的调度方式 
  61.      */  
  62.     private TaskDispatcher(int threadCount, Type type) {  
  63.         init(threadCount, type);  
  64.     }  
  65.   
  66.     /** 
  67.      * 初始化 
  68.      * @param threadCount    线程池的线程数量 
  69.      * @param type           队列的调度方式 
  70.      */  
  71.     private void init(int threadCount, Type type) {  
  72.   
  73.         mThreadPool = Executors.newFixedThreadPool(threadCount);  
  74.         mPollingSemaphore = new Semaphore(threadCount);  
  75.         mTaskList = new LinkedList<Runnable>();  
  76.         mType = type == null ? Type.LIFO : type;  
  77.   
  78.         // 开启轮询线程  
  79.         mPollingThead = new Thread() {  
  80.             @Override  
  81.             public void run() {  
  82.                 Looper.prepare();  
  83.                 mPollingHanler = new Handler() {  
  84.                     @Override  
  85.                     public void handleMessage(Message msg) {  
  86.                         mThreadPool.execute(getTask());  
  87.                         try {  
  88.                             mPollingSemaphore.acquire();  
  89.                         } catch (InterruptedException e) {  
  90.                             e.printStackTrace();  
  91.                         }  
  92.                     }  
  93.                 };  
  94.                 mSemaphore.release();   // 释放一个信号量  
  95.                 Looper.loop();  
  96.             }  
  97.         };  
  98.         mPollingThead.start();  
  99.     }  
  100.   
  101.     /** 
  102.      * 添加一个任务 
  103.      * @param task   任务 
  104.      */  
  105.     private synchronized void addTask(Runnable task) {  
  106.         try {  
  107.             // mPollingHanler为空时,请求信号量,因为mPollingHanler创建完成会释放一个信号量  
  108.             if (mPollingHanler == null) mSemaphore.acquire();  
  109.         } catch (InterruptedException e) {  
  110.             e.printStackTrace();  
  111.         }  
  112.         mTaskList.add(task);  
  113.   
  114.         mPollingHanler.sendEmptyMessage(0x110);  
  115.     }  
  116.   
  117.     /** 
  118.      * 取出一个任务 
  119.      * @return 需要执行的任务 
  120.      */  
  121.     private synchronized Runnable getTask() {  
  122.         if (mType == Type.LIFO) {  
  123.             return mTaskList.removeLast();  
  124.         } else if (mType == Type.FIFO) {  
  125.             return mTaskList.removeFirst();  
  126.         }  
  127.         return null;  
  128.     }  
  129.   
  130.     /** 
  131.      * 执行自定义的任务 
  132.      */  
  133.     public void doTask() {  
  134.         addTask(new Runnable() {  
  135.             @Override  
  136.             public void run() {  
  137.   
  138.                 // Todo what you like  
  139.   
  140.                 mPollingSemaphore.release();  //这句必须有,任务处理完成后释放一个信号量  
  141.             }  
  142.         });  
  143.     }  
  144. }  

注释都比较详细了,使用起来也很简单

  1. TaskDispatcher.getInstance().doTask();  

详细分析,日后补充。

转自 http://blog.csdn.net/u010403463/article/details/47274147

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值