android 多线程 - 线程池 Executors.newFixedThreadPool 的使用例子

  1. class ExecutorThread implements Runnable {     
  2.     private final String name;  
  3.     MutliPageItemView mItemView;  
  4.     MutliPageAdapter mData;  
  5.       
  6.     //初始化参数(根据需要修改)  
  7.     public ExecutorThread(String name, MutliPageItemView item, MutliPageAdapter mData) {     
  8.         this.name = name;  
  9.         this.mItemView = item;  
  10.         this.mData = mData;  
  11.     }  
  12.     
  13.   //运行处理线程(根据需要增加自己的处理)  
  14.     public void run() {  
  15.         System.out.println("启动: " + name);     
  16.         try {  
  17.               Thread.sleep(50000);  
  18.         } catch (InterruptedException e) {  
  19.             // TODO Auto-generated catch block  
  20.             e.printStackTrace();  
  21.         }     
  22.         System.out.println("完成: " + name);     
  23.     }   
  24.   
  25.     public static class LoadDataExecutor {  
  26.         ExecutorService executor;     
  27.         MutliPageItemView mItemView;  
  28.         MutliPageAdapter mDataAdapter;  
  29.         public static void main(String[] args) {  
  30.         }  
  31.        
  32.      //初始化参数(根据自己的需要修改参数)  
  33.         public void init(MutliPageAdapter mData)  
  34.         {  
  35.             executor = Executors.newFixedThreadPool(MutliPageAdapter.THREAD_COUNT);    
  36.             mDataAdapter = mData;  
  37.         }  
  38.       
  39.         //运行线程(根据需要修改自己的参数)  
  40.         public void runThread(String name, MutliPageItemView itemView)  
  41.         {  
  42.             mItemView = itemView;  
  43.             Runnable runner = new ExecutorThread(name,itemView,mDataAdapter);  
  44.             executor.execute(runner);  
  45.         }  
  46.       
  47.         //关闭线程  
  48.         public void closeThread()  
  49.         {        
  50.             //Thread.sleep(waitTime);    
  51.             if(executor!=null)  
  52.             {  
  53.                 try {  
  54.                     System.out.println("关闭线程。 ");   
  55.                     //executor.shutdown();     
  56.                     executor.shutdownNow();  
  57.                     executor.awaitTermination(0, TimeUnit.NANOSECONDS);     
  58.                 } catch (InterruptedException ignored)   
  59.                 {                 
  60.                 }     
  61.                   
  62.             }  
  63.         }  
  64.     }  
  65. }  
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.   

调用方法:



LoadDataExecutor mLoadDataThread;


mLoadDataThread = new LoadDataExecutor();
mLoadDataThread.init(this);


//增加处理

mLoadDataThread.runThread("id:"+ id,itemView);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值