Android 学习笔记--android――AsyncTask在Android4.X的机制问题

AsyncTask在Android4.X的机制问题

       AsyncTask在Android4.X机制是串行的...单一AsyncTask运行完才会运行下一个

       AsyncTask在Android2.3机制是并行的..最多支持5个

 


解决AsyncTask 在4.x串行的问题...自己编写线程池


public class PoolThread {
    private static final int CPU_COUNT = Runtime.getRuntime()
            .availableProcessors();
    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 10 + 1;
    private static final int KEEP_ALIVE = 1;
    private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(
            128);
    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
        }
    };
    public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
            CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS,
            sPoolWorkQueue, sThreadFactory);

}


启动 AsyncTask 的时候  reTask.executeOnExecutor(PoolThread.THREAD_POOL_EXECUTOR,params...)

就可以并行咯..

你可能感兴趣的:(Android 学习笔记--android――AsyncTask在Android4.X的机制问题)