package com.czbix.v2ex.util;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ExecutorUtils {
    private static final ExecutorService CACHED_POOL = Executors.newCachedThreadPool();
    private static final ScheduledExecutorService SCHEDULED_POOL = Executors.newScheduledThreadPool(0);
    private static final Handler UI_HANDLER = new Handler(Looper.getMainLooper());

    /**
     * @see ExecutorService#submit(Callable)
     */
    public static <T> Future<T> submit(Callable<T> task) {
        return CACHED_POOL.submit(task);
    }

    /**
     * @see ExecutorService#submit(Runnable)
     */
    public static Future<?> submit(Runnable task) {
        return CACHED_POOL.submit(task);
    }

    /**
     * @see ExecutorService#submit(Runnable, Object)
     */
    public static <T> Future<T> submit(Runnable task, T result) {
        return CACHED_POOL.submit(task, result);
    }

    /**
     * @see ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
     */
    public static ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
        return SCHEDULED_POOL.scheduleWithFixedDelay(command, initialDelay, delay, unit);
    }

    /**
     * @see ScheduledExecutorService#schedule(Runnable, long, TimeUnit)
     */
    public static ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
        return SCHEDULED_POOL.schedule(command, delay, unit);
    }

    /**
     * @see ScheduledExecutorService#schedule(Callable, long, TimeUnit)
     */
    public static <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
        return SCHEDULED_POOL.schedule(callable, delay, unit);
    }

    /**
     * @see ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit)
     */
    public static ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
        return SCHEDULED_POOL.scheduleAtFixedRate(command, initialDelay, period, unit);
    }

    /**
     * @see ExecutorService#execute(Runnable)
     */
    public static void execute(Runnable command) {
        CACHED_POOL.execute(command);
    }

    public static void runInUiThread(Runnable runnable) {
        UI_HANDLER.post(runnable);
    }
}

@@ -326,32 +326,89 @@
 cheduledFuture;%0A
+import java.util.concurrent.ScheduledThreadPoolExecutor;%0A
 import java.util
@@ -488,69 +488,79 @@
 nal 
-ExecutorService CACHED_POOL = Executors.newCachedThread
+Handler mUiHandler;%0A    private static final ExecutorService mCache
 Pool
-()
 ;%0A  
@@ -607,30 +607,122 @@
 Service 
-SCHEDULED_POOL
+mScheduledPool;%0A%0A    static %7B%0A        mUiHandler = new Handler(Looper.getMainLooper());%0A        mCachePool
  = Execu
@@ -729,23 +729,20 @@
 tors.new
-Schedul
+Cach
 edThread
@@ -750,94 +750,405 @@
 ool(
-0
 );%0A
+%0A 
     
-private static final Handler UI_HANDLER = new Handler(Looper.getMainLooper());
+   // JELLY_BEAN(16) has bug that can't set core pool size to 0, otherwise it will not create%0A        // thread to run task.%0A        mScheduledPool = Executors.newScheduledThreadPool(4);%0A        ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) mScheduledPool;%0A        executor.setKeepAliveTime(3, TimeUnit.SECONDS);%0A        executor.allowCoreThreadTimeOut(true);%0A    %7D
 %0A%0A  
@@ -1276,35 +1276,34 @@
      return 
-CACHED_POOL
+mCachePool
 .submit(task
@@ -1436,35 +1436,34 @@
      return 
-CACHED_POOL
+mCachePool
 .submit(task
@@ -1622,27 +1622,26 @@
  return 
-CACHED_POOL
+mCachePool
 .submit(
@@ -1907,38 +1907,38 @@
      return 
-SCHEDULED_POOL
+mScheduledPool
 .scheduleWit
@@ -2185,38 +2185,38 @@
      return 
-SCHEDULED_POOL
+mScheduledPool
 .schedule(co
@@ -2443,38 +2443,38 @@
      return 
-SCHEDULED_POOL
+mScheduledPool
 .schedule(ca
@@ -2750,22 +2750,22 @@
 urn 
-SCHEDULED_POOL
+mScheduledPool
 .sch
@@ -2951,19 +2951,18 @@
     
-CACHED_POOL
+mCachePool
 .exe
@@ -3053,18 +3053,18 @@
     
-UI_HANDLER
+mUiHandler
 .pos

fix bug of ScheduledThreadPoolExecutor on JB_MR1