- class ExecutorThread implements Runnable {
- private final String name;
- MutliPageItemView mItemView;
- MutliPageAdapter mData;
- //初始化参数(根据需要修改)
- public ExecutorThread(String name, MutliPageItemView item, MutliPageAdapter mData) {
- this.name = name;
- this.mItemView = item;
- this.mData = mData;
- }
- //运行处理线程(根据需要增加自己的处理)
- public void run() {
- System.out.println("启动: " + name);
- try {
- Thread.sleep(50000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("完成: " + name);
- }
- public static class LoadDataExecutor {
- ExecutorService executor;
- MutliPageItemView mItemView;
- MutliPageAdapter mDataAdapter;
- public static void main(String[] args) {
- }
- //初始化参数(根据自己的需要修改参数)
- public void init(MutliPageAdapter mData)
- {
- executor = Executors.newFixedThreadPool(MutliPageAdapter.THREAD_COUNT);
- mDataAdapter = mData;
- }
- //运行线程(根据需要修改自己的参数)
- public void runThread(String name, MutliPageItemView itemView)
- {
- mItemView = itemView;
- Runnable runner = new ExecutorThread(name,itemView,mDataAdapter);
- executor.execute(runner);
- }
- //关闭线程
- public void closeThread()
- {
- //Thread.sleep(waitTime);
- if(executor!=null)
- {
- try {
- System.out.println("关闭线程。 ");
- //executor.shutdown();
- executor.shutdownNow();
- executor.awaitTermination(0, TimeUnit.NANOSECONDS);
- } catch (InterruptedException ignored)
- {
- }
- }
- }
- }
- }
调用方法:
LoadDataExecutor mLoadDataThread;
mLoadDataThread = new LoadDataExecutor();
mLoadDataThread.init(this);
//增加处理
mLoadDataThread.runThread("id:"+ id,itemView);