접기
package com.mdiwebma.base.task;
import java.util.ArrayDeque;import java.util.concurrent.Executor;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;import android.os.Handler;import android.os.Looper;import android.support.annotation.NonNull ;import com.mdiwebma.base.BuildConfig;import com.mdiwebma.base.helper.StringBuilderPool;import com.mdiwebma.base.logging.Dlog;public class CommonExecutors { private static ExecutorService cachedThreadPool = Executors.newCachedThreadPool (); private static SerialExecutor serialExecutor = new SerialExecutor(); private static ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor (); private static Handler handler = new Handler(Looper.getMainLooper ());; // from AsyncTask private static class SerialExecutor implements Executor { final ArrayDeque<Runnable> tasks = new ArrayDeque<Runnable>(); Runnable activeRunnable ; public synchronized void execute(final Runnable runnable) { tasks .offer(new Runnable() { public void run() { try { runnable .run(); } finally { scheduleNext(); } } }); if (activeRunnable == null ) { scheduleNext(); } } protected synchronized void scheduleNext() { if ((activeRunnable = tasks .poll()) != null ) { cachedThreadPool .execute(activeRunnable ); } } } private CommonExecutors() { } @NonNull public static ExecutorService getCachedThreadPool() { return cachedThreadPool ; } @NonNull public static Handler getHandler() { return handler ; } public static void execute(@NonNull Runnable runnable) { cachedThreadPool .execute(new SafeRunnable(runnable, getCallerStack ())); } private static class SafeRunnable implements Runnable { final Runnable innerRunnable ; final String callerStack ; SafeRunnable(Runnable innerRunnable, String callerStack) { this .innerRunnable = innerRunnable; this .callerStack = callerStack; } @Override public void run() { try { innerRunnable .run(); } catch (OutOfMemoryError err) { System.gc (); Dlog.notReached (err, callerStack ); } catch (Exception ex) { Dlog.notReached (ex, callerStack ); Dlog.sendLog ("Crash" , callerStack , ex.toString()); } } } private static String getCallerStack() { String callerStack = null ; if (BuildConfig.DEBUG ) { int lineCount = 0 ; StringBuilder sb = StringBuilderPool.obtain (); for (StackTraceElement e : Thread.currentThread ().getStackTrace()) { if (e.getClassName().equals("dalvik.system.VMStack" ) == false && e.getClassName().equals("java.lang.Thread" ) == false && e.getClassName().equals(CommonExecutors.class .getName()) == false ) { sb.append(e.toString()); sb.append(" \n " ); lineCount++; if (lineCount > 5 ) { break ; } } } callerStack = sb.toString(); StringBuilderPool.recycle (sb); } return callerStack; } public static void executeBySerial(@NonNull Runnable runnable) { serialExecutor .execute(new SafeRunnable(runnable, getCallerStack ())); } public static void executeBySchedule(@NonNull Runnable runnable, long delay, TimeUnit timeUnit) { scheduledThreadPool .schedule(new SafeRunnable(runnable, getCallerStack ()), delay, timeUnit); } }
접기 접기
void test2() {
CommonExecutors.executeBySchedule (new Runnable() { @Override public void run() { Dlog.l ("run..sc" ); SystemClock.sleep (3000 ); throw new RuntimeException("test1111" ); } }, 5 , TimeUnit.SECONDS ); CommonExecutors.executeBySerial (new Runnable() { @Override public void run() { Dlog.l ("run.." ); SystemClock.sleep (3000 ); throw new RuntimeException("test1111" ); } }); CommonExecutors.executeBySerial (new Runnable() { @Override public void run() { Dlog.l ("run.." ); SystemClock.sleep (3000 ); throw new RuntimeException("test1112" ); } }); CommonExecutors.executeBySerial (new Runnable() { @Override public void run() { Dlog.l ("run.." ); SystemClock.sleep (3000 ); throw new RuntimeException("test1113" ); } }); CommonExecutors.executeBySerial (new Runnable() { @Override public void run() { Dlog.l ("run.." ); SystemClock.sleep (3000 ); throw new RuntimeException("test1114" ); } }); }
접기