/** * Execute a task. * * @param runnable the task to execute * @param id identifier used for task cancellation * @param delay the time from now to delay execution, in milliseconds * @param serial the serial queue (<code>null</code> or <code>""</code> for no serial execution) * @throws IllegalArgumentException if <code>delay</code> is strictly positive and the current * executor does not support scheduling (if {@link #setExecutor(Executor)} has been called * with such an executor) */ public static void execute(final Runnable runnable, String id, int delay, String serial) { execute( new Task(id, delay, serial) { @Override public void execute() { runnable.run(); } }); }
/** 启动自动下载数据 */ public void autoDownLoadDataTask() { BackgroundExecutor.execute( new BackgroundExecutor.Task("", 500, "") { @Override public void execute() { try { getSDCardDataFromCamera(); } catch (Throwable e) { Thread.getDefaultUncaughtExceptionHandler() .uncaughtException(Thread.currentThread(), e); } } }); }
@Override public void loadNewList(final String url) { BackgroundExecutor.execute( new BackgroundExecutor.Task("", 0, "") { @Override public void execute() { try { DianYingFragment_.super.loadNewList(url); } catch (Throwable e) { Thread.getDefaultUncaughtExceptionHandler() .uncaughtException(Thread.currentThread(), e); } } }); }
private void postExecute() { if (id == null && serial == null) { /* nothing to do */ return; } CURRENT_SERIAL.set(null); synchronized (BackgroundExecutor.class) { /* execution complete */ TASKS.remove(this); if (serial != null) { Task next = take(serial); if (next != null) { if (next.remainingDelay != 0) { /* the delay may not have elapsed yet */ next.remainingDelay = Math.max(0, (int) (targetTimeMillis - System.currentTimeMillis())); } /* a task having the same serial was queued, execute it */ BackgroundExecutor.execute(next); } } } }
/** * Execute a task after all tasks added with the same non-null <code>serial</code> (if any) have * completed execution. * * <p>Equivalent to {@link #execute(Runnable, String, int, String) execute(runnable, id, 0, * serial)}. * * @param runnable the task to execute * @param id identifier used for task cancellation * @param serial the serial queue to use (<code>null</code> or <code>""</code> for no serial * execution) */ public static void execute(Runnable runnable, String id, String serial) { execute(runnable, id, 0, serial); }