想起从来没有写过博客,把刚搞出来的一个页面多个请求的处理方式的思路放上来,避免以后忘记。
首先用的是 Future+excecutorService 方式
其次 我各自封装了几个类来方便实现
类:Takgroup 用于存放所有future回调线程的工具类
public class TaskGroup implements Runnable {
private Collection<Future<?>> mustFutures = CollectionUtil.newArrayList();
private Collection<Future<?>> mayFutures = CollectionUtil.newArrayList();
private Collection<Runnable> mustTasks = CollectionUtil.newArrayList();
private Collection<Runnable> mayTasks = CollectionUtil.newArrayList();
private Collection<Callable<?>> mustCallTask = CollectionUtil.newArrayList();
private ExecutorService executorService = Executors.newCachedThreadPool();
//此类没多少可看的东西,只是统一管理。
public Collection<Future<?>> getMayFutures() {
return mayFutures;
}
public void setMayFutures(Collection<Future<?>> mayFutures) {
this.mayFutures = mayFutures;
}
public void addMust(Runnable mustTask) {
mustTasks.add(mustTask);
}
public void addMay(Runnable mayTask) {
mayTasks.add(mayTask);
}
public void addMustFutres(Future<?> future){
mustFutures.add(future);
}
public void addMayFutres(Future<?> future){
mayFutures.add(future);
}
public void addCallable(Callable<?> call){
mustCallTask.add(call);
}
@Override
public void run() {
for(Runnable mustTask : mustTasks) {
mustFutures.add(executorService.submit(mustTask));
}
for(Runnable mayTask : mayTasks) {
mayFutures.add(executorService.submit(mayTask));
}
for(Callable<?> call :mustCallTask){
executorService.submit(call);
}
}
public void shutdown() {
// 关闭线程池
executorService.shutdown();
}
}
然后接下来是一个继承了回调接口的线程类:NewThreadHelper
public class NewThreadHelper implements Callable<Response> {
private Dialog dialog;//自定义dialog 方便网络处理的时候的缓冲画面
private Request request;//自己封装的网络请求类
public NewThreadHelper(Dialog dialog, Request request) {
super();
this.dialog = dialog;
this.request = request;
}
@Override
public Response call() throws Exception {
try {
//manager是请求发起者
RemoteManager remoteManager = RemoteManager.getFullFeatureRemoteManager();
//开始执行网络处理
Response response = remoteManager.execute(request);
Thread.sleep(200);
return response;
} catch (Exception e) {
Log.e("ThreadHelper", e.getMessage(), e);
throw new RuntimeException(e);
} finally {
dialog.dismiss();
}
}
}
最后一个最关键的 页面调用
声明 private TaskGroup taskGroup;
//网络请求方法 随便写在哪儿都行。
public void request(){
Dialog progressDialog = showNewDialog("正在处理网络请求.");
progressDialog.show();
progressDialog.setOnDismissListener(new TestDismissListener());
for(int i = 1;i<5;i++){
//此处写request的具体请求.......
taskGroup.addMayFutres(chenApplication.asyInvoke(new NewThreadHelper(progressDialog, request)));
}
//调用....
chenApplication.asyCall(taskGroup);
}
下面这个 TestDismissListener 类 是整个过程收尾的一个处理结果类,监听Dialog的onDismiss方法,此次taskGroup里面的每一个future的get结果返回在这个类里面,整个过程处理结束 则会通过NewThreadHelper类的回调调用此监听类,让Dialog消失 然后 更新数据 刷新界面。
class TestDismissListener implements DialogInterface.OnDismissListener{
@Override
public void onDismiss(DialogInterface dialog) {
for(Future<?> result : taskGroup.getMayFutures()){
Response response = null;
if(result!=null){
try {
response = (Response) result.get();
logError(response.toString());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
此博文完全是加强自身记忆力,整个处理中 有很多细小的类 没办法贴上来了。介绍了一个思路,发起future请求,弹出dialog 请求结束 dialog消失。不喜勿喷,当然产品设计在最初的时候应该尽量避免一个页面同时多个请求的情况发生,页面的初始化工作,最好能以一个缓冲画面来代替,这样同时有多个互相不影响的请求就可以部分顺序的加载然后刷新整个页面。