创建单个线程池的方法
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class YhshThreadPoolFactory {
private ExecutorService mThreadService = null;
private YhshThreadPoolFactory() {
mThreadService = Executors.newSingleThreadExecutor();
}
private static class YhshThreadPoolFactoryHolder {
private static final YhshThreadPoolFactory INSTANCE = new YhshThreadPoolFactory();
}
public static YhshThreadPoolFactory getInstance() {
return YhshThreadPoolFactoryHolder.INSTANCE;
}
public <T> Future<T> submitRequest(Runnable runnable, T result) {
return mThreadService.submit(runnable, result);
}
public void executeRequest(Runnable runnable) {
mThreadService.execute(runnable);
}
}
下面是调用线程的方法,网络请求写在run里面即可
YhshThreadPoolFactory.getInstance().executeRequest(new Runnable() {
@Override
public void run() {
//写相应的网络请求逻辑即可
}
});