根据大佬的https://my.oschina.net/oahcfly/blog/369432而改进的,主要增加了超时,和报错抓取
package com.zhfy.game.framework.tool; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.ObjectMap; import com.badlogic.gdx.utils.async.AsyncExecutor; import com.zhfy.game.MainGame; import com.zhfy.game.config.ResDefaultConfig; import com.zhfy.game.framework.ComUtil; import com.zhfy.game.model.content.conversion.Fb2Smap; import java.lang.reflect.Field; import java.util.Stack; public class CHAsyncManager implements Disposable { MainGame game; // 任务堆栈 private final Stack<CHAsyncTask> tasks = new Stack<CHAsyncTask>(); private AsyncExecutor executor; private int taskMax; // private ObjectMap<String, CHAsyncTask> checks; public CHAsyncManager(MainGame game) { // 池中允许的最大线程数1。超过最大上限会等待前面的执行完才接着执行。 executor = new AsyncExecutor(1); this.game=game; //checks=new ObjectMap<>(); } public boolean update() { if (tasks.size() == 0){ taskMax=0; //checks.clear(); return true; } // Gdx.app.log("CHAsyncTask",tasks.size()+":"+tasks.get(0).getContent()); return updateTask() && tasks.size() == 0; } private boolean updateTask() { try { //任务更新情况 CHAsyncTask task = tasks.peek(); if (task.update(game)) { /* if(!ComUtil.isEmpty(task.getContent())){ checks.remove(task.getContent()); }*/ // 处理完毕了,移除掉 tasks.pop(); return true; } } catch (Exception e) { if(ResDefaultConfig.ifDebug){ e.printStackTrace(); }else{ game.recordLog("MainGame render ",e); CHAsyncTask task = tasks.peek(); if (task.update(game)) { /* if (!ComUtil.isEmpty(task.getContent())) { checks.remove(task.getContent()); }*/ // 处理完毕了,移除掉 tasks.pop(); return true; } } } return false; } /** *预先加载任务 */ public void loadTask(CHAsyncTask task) { if (task.getAsyncManager() == null) { task.setAsyncManager(this); } /* if(!ComUtil.isEmpty(task.getContent())){ if(checks.containsKey(task.getContent())){//清理之前的任务 CHAsyncTask ot=checks.remove(task.getContent()); if(ot!=null){ tasks.remove(ot); } } checks.put(task.getContent(),task); }*/ tasks.push(task); taskMax=tasks.size(); Gdx.app.log("loadTask:"+task.getContent(),taskMax+""); } @Override public void dispose() { executor.dispose(); } public AsyncExecutor getExecutor() { return executor; } public float getProgress(){ if(taskMax==0){ return 1f; }else { return 1-((tasks.size()*1f)/taskMax); } } public int getTaskSize(){ return tasks.size(); } public void clearTask(){ //checks.clear(); taskMax=0; tasks.clear(); } public void logTask() { for(CHAsyncTask task:tasks){ Gdx.app.log("task:",task.getContent()+":"+task.getTimeInfo()); } } }
1 package com.zhfy.game.framework.tool; 2 3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.utils.GdxRuntimeException; 5 import com.badlogic.gdx.utils.async.AsyncResult; 6 import com.badlogic.gdx.utils.async.AsyncTask; 7 import com.zhfy.game.MainGame; 8 import com.zhfy.game.config.ResDefaultConfig; 9 import com.zhfy.game.framework.GameUtil; 10 11 public abstract class CHAsyncTask implements AsyncTask<String> { 12 private CHAsyncManager asyncManager; 13 // 处理结果 14 private AsyncResult<String> depsFuture = null; 15 16 // 处理OK? 17 volatile boolean asyncDone = false; 18 19 private float timeMax;//毫秒 0标示不限制事件 20 private float timeSum; 21 private String content; 22 23 public String getContent(){ 24 return content; 25 } 26 27 public CHAsyncTask(String content,float timeMax) { 28 if(timeMax!=-1){ 29 this.timeMax=timeMax; 30 } 31 timeSum=0; 32 this.content=content; 33 } 34 35 public CHAsyncTask(CHAsyncManager manager) { 36 asyncManager = manager; 37 timeSum=0; 38 timeMax=0; 39 } 40 41 @Override 42 public String call() throws Exception { 43 44 try { 45 // 执行任务 46 String result = doInBackground(); 47 asyncDone = true; 48 return result; 49 } catch (Exception e) { 50 if (ResDefaultConfig.ifDebug) { 51 e.printStackTrace(); // throw new GdxRuntimeException("depsFuture.get() failed!!!!"); 52 } 53 } 54 return null; 55 } 56 57 /**开始执行*/ 58 public abstract void onPreExecute(); 59 60 /**执行结束返回结果*/ 61 public abstract void onPostExecute(String result); 62 63 /**异步执行*/ 64 public abstract String doInBackground(); 65 66 public Boolean ifTimeOut(){ 67 if(timeMax>0&&timeSum>timeMax){ 68 return true; 69 } 70 return false; 71 } 72 73 74 // 75 public boolean update(MainGame game) { 76 77 if(timeMax>0){ 78 timeSum+=Gdx.graphics.getDeltaTime(); 79 } 80 if(ifTimeOut()){ 81 Gdx.app.error("任务执行已超时 "+timeSum+":"+timeMax,getContent()); 82 depsFuture = asyncManager.getExecutor().submit(this); 83 asyncDone=true; 84 }else if (!asyncDone) { 85 //没有执行完成 86 if (depsFuture == null) { 87 // 也没有提交任务,进行提交 88 try { 89 onPreExecute(); 90 depsFuture = asyncManager.getExecutor().submit(this); 91 } catch (Exception e) { 92 if (ResDefaultConfig.ifDebug) { 93 e.printStackTrace(); // throw new GdxRuntimeException("depsFuture.get() failed!!!!"); 94 } else if (!game.gameConfig.getIfIgnoreBug()) { 95 game.remindBugFeedBack(); 96 } 97 game.recordLog("CHAsyncTask update1 ", e); 98 } 99 } 100 } else { 101 if (depsFuture.isDone()) { 102 // 通过结果发现执行完毕了 103 try { 104 onPostExecute(depsFuture.get()); 105 } catch (Exception e) { 106 if (ResDefaultConfig.ifDebug) { 107 e.printStackTrace(); // throw new GdxRuntimeException("depsFuture.get() failed!!!!"); 108 } else if ( !game.gameConfig.getIfIgnoreBug()) { 109 game.remindBugFeedBack(); 110 } 111 game.recordLog("CHAsyncTask update2 ", e); 112 } 113 114 } 115 }/*if (!asyncDone) { 116 //没有执行完成 117 if (depsFuture == null) { 118 // 也没有提交任务,进行提交 119 try { 120 onPreExecute(); 121 depsFuture = asyncManager.getExecutor().submit(this); 122 } catch (Exception e) { 123 if(ResDefaultConfig.ifDebug){ 124 e.printStackTrace(); // throw new GdxRuntimeException("depsFuture.get() failed!!!!"); 125 }else if(!game.gameConfig.getIfIgnoreBug()){ 126 game.remindBugFeedBack(); 127 } 128 game.recordLog("CHAsyncTask update1 ",e); 129 } 130 }else { 131 if (depsFuture!=null&&depsFuture.isDone()) { 132 // 通过结果发现执行完毕了 133 try { 134 onPostExecute(depsFuture.get()); 135 } catch (Exception e) { 136 if (ResDefaultConfig.ifDebug) { 137 e.printStackTrace(); // throw new GdxRuntimeException("depsFuture.get() failed!!!!"); 138 } else if (!game.gameConfig.getIfIgnoreBug()) { 139 game.remindBugFeedBack(); 140 } 141 game.recordLog("CHAsyncTask update2 ", e); 142 } 143 asyncDone = true; 144 }else{ 145 try { 146 onPreExecute(); 147 depsFuture = asyncManager.getExecutor().submit(this); 148 } catch (Exception e) { 149 if(ResDefaultConfig.ifDebug){ 150 e.printStackTrace(); // throw new GdxRuntimeException("depsFuture.get() failed!!!!"); 151 }else if(!game.gameConfig.getIfIgnoreBug()){ 152 game.remindBugFeedBack(); 153 } 154 game.recordLog("CHAsyncTask update3 ",e); 155 } 156 } 157 } 158 } else { 159 if (depsFuture!=null&&depsFuture.isDone()) { 160 // 通过结果发现执行完毕了 161 try { 162 onPostExecute(depsFuture.get()); 163 } catch (Exception e) { 164 if(ResDefaultConfig.ifDebug){ 165 e.printStackTrace(); // throw new GdxRuntimeException("depsFuture.get() failed!!!!"); 166 }else if(!game.gameConfig.getIfIgnoreBug()){ 167 game.remindBugFeedBack(); 168 } 169 game.recordLog("CHAsyncTask update4 ",e); 170 } 171 } 172 }*/ 173 return asyncDone; 174 } 175 176 public void setAsyncManager(CHAsyncManager asyncManager) { 177 this.asyncManager = asyncManager; 178 } 179 180 public CHAsyncManager getAsyncManager() { 181 return asyncManager; 182 } 183 184 /** 185 * 186 * <pre> 187 * 是否执行完毕 188 * 189 * date: 2015-1-18 190 * </pre> 191 * @author 192 * @return 193 */ 194 public boolean isDone() { 195 return asyncDone; 196 } 197 198 public String getTimeInfo(){ 199 return timeSum+"/"+timeMax; 200 } 201 }
//转化抓取的错误信息 public static String ex2String(Throwable t) { StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw, true)); return sw.getBuffer().toString(); } //写出错误信息到日志 public static void recordLog(String logStr) { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); Date date = new Date(System.currentTimeMillis()); FileHandle file =null; if(Gdx.app.getType()== Application.ApplicationType.Desktop){ file = Gdx.files.local(ResDefaultConfig.Path.LogFolderPath+ResDefaultConfig.game + "_" + formatter.format(date) + ".log"); }else{ file = Gdx.files.external(ResDefaultConfig.game + "_" + formatter.format(date) + ".log"); } file.writeString("\n" + logStr, true); }
使用方法
1 final CHAsyncTask task = new CHAsyncTask("save",999) { 2 final Fb2Smap fb2Smap=sMapDAO; 3 @Override 4 public void onPreExecute() { 5 //前置处理 6 } 7 8 @Override 9 public void onPostExecute(String result) { 10 //根据结果后再处理 11 } 12 @Override 13 public String doInBackground() { 14 //异步处理,返回结果 15 return null; 16 } 17 }; 18 asyncManager.loadTask(task); 19 asyncManager.update();