
/*** 执行异步任务* 超时时间* 超过此时间,则打印任务在队列中的时间* @param group 任务队列类型* @param handler 任务handler*/public void run(Group group, IAsyncHandler handler) {AsyncTask task = new AsyncTask(taskTimeout, handler);balance(group, task);}/*** 构造异步任务* @param timeout 超时时间(单位:豪秒)* @param handler 执行句柄*/public AsyncTask(int timeout, IAsyncHandler handler) {super();if (timeout < 0) {timeout = 1000;}this.timeout = timeout;this.handler = handler;this.addTime = System.currentTimeMillis();}/*** 任务执行**/public void run() {while (!isStop) {AsyncTask task = null;try {task = taskQueue.poll(1500, TimeUnit.MILLISECONDS);if (null != task) {//重点执行语句:执行超时丢弃的任务队列execTimeoutTask(task);}} catch (InterruptedException ie) {logger.error("has error !", ie);} catch (Throwable ex) {if (task != null) {task.getHandler().exceptionCaught(ex);}}}}/*** @任务超时处理* @param task* @throws Throwable*/private void execTimeoutTask(AsyncTask task) throws Throwable {if ((System.currentTimeMillis() - task.getAddTime()) > task.getQtimeout()) {task.getHandler().exceptionCaught(new TimeoutException(threadFactoryName + "async task timeout!"));return;} else {Object obj = task.getHandler().run();task.getHandler().messageReceived(obj);}}
文章转载自老李说架构之道,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




