android开发 用线程进行耗时操作,统一处理

最近项目中,发现程序的反应实在有点慢,主线程中的耗时操作多了点,但没有时间去改,就想写个公用类,专门用来处理这些耗时操作,于是写了个 ThreadUtil,

思路是在util中使用一个单例线程池,然后用反射的方法去调用需要被调用的方法,然后用handler去协调,代码并没优美,只是把想法记录下来,以后再进行优化。代码如下:


ThreadUtil.java

package com.bjj.test_thread_step;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2016/4/18.
 */
public class ThreadUtil {

    private ExecutorService services;

    public static ThreadUtil thread;

    public static ThreadUtil getInstance(){
        if(thread == null){
            thread = new ThreadUtil();
        }
        return  thread;
    }

    public ThreadUtil(){
        services = Executors.newSingleThreadExecutor();
    }

    public void add(final Method method,final Object _class){
        services.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    method.invoke(_class);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

MainActivity.java

用Thread.sleep(10000) 来模拟耗时操作

package com.bjj.test_thread_step;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:{
                    try {
                        ThreadUtil.getInstance().add(MainActivity.this.getClass().getMethod("step1"),MainActivity.this);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                    break;
                }
                case 1:{
                    try {
                        ThreadUtil.getInstance().add(MainActivity.this.getClass().getMethod("step2"),MainActivity.this);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                    break;
                }
                case 2:{
                    try {
                        ThreadUtil.getInstance().add(MainActivity.this.getClass().getMethod("step3"),MainActivity.this);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                    break;
                }
                case 3:{
                    try {
                        ThreadUtil.getInstance().add(MainActivity.this.getClass().getMethod("step4"),MainActivity.this);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(0);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void step1(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ((TextView) findViewById(R.id.t1)).setText("这是第一步");
            }
        });
        handler.sendEmptyMessage(1);
    }

    public void step2(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ( (TextView)findViewById(R.id.t2) ).setText("这是第二步");
            }
        });
        handler.sendEmptyMessage(2);
    }

    public void step3(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ( (TextView)findViewById(R.id.t3) ).setText("这是第三步");
            }
        });
        handler.sendEmptyMessage(3);
    }

    public void step4(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ( (TextView)findViewById(R.id.t4) ).setText("这是第四步");
            }
        });
    }
}


你可能感兴趣的:(android)