If you think the Android project cube-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package in.srain.cube.image.impl;
//fromwww.java2s.comimport android.annotation.TargetApi;
import android.os.Build;
import in.srain.cube.concurrent.LinkedBlockingDeque;
import in.srain.cube.image.ImageLoader.ImageTaskOrder;
import in.srain.cube.image.iface.ImageTaskExecutor;
import in.srain.cube.util.Version;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Use a Thread pool to manager the thread.
*
* @author http://www.liaohuqiu.net
*/publicclass DefaultImageTaskExecutor implements ImageTaskExecutor {
privatestaticfinal TimeUnit KEEP_ALIVE_TIME_UNIT;
privatestaticfinalint KEEP_ALIVE_TIME = 1;
privatestaticint sNUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
privatestatic DefaultImageTaskExecutor sInstance = null;
privatefinal ThreadPoolExecutor mThreadPool;
privatefinal LinkedBlockingStack<Runnable> mTaskWorkQueue;
static {
KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
sInstance = new DefaultImageTaskExecutor();
}
publicstatic DefaultImageTaskExecutor getInstance() {
return sInstance;
}
publicstaticclass LinkedBlockingStack<T> extends LinkedBlockingDeque<T> {
privatestaticfinallong serialVersionUID = -4114786347960826192L;
private ImageTaskOrder mImageTaskOrder = ImageTaskOrder.FIRST_IN_FIRST_OUT;
publicvoid setTaskOrder(ImageTaskOrder order) {
mImageTaskOrder = order;
}
@Override
publicboolean offer(T e) {
if (mImageTaskOrder == ImageTaskOrder.LAST_IN_FIRST_OUT) {
return super.offerFirst(e);
} else {
return super.offer(e);
}
}
@Override
public T remove() {
if (mImageTaskOrder == ImageTaskOrder.LAST_IN_FIRST_OUT) {
return super.removeFirst();
} else {
return super.remove();
}
}
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private DefaultImageTaskExecutor() {
mTaskWorkQueue = new LinkedBlockingStack<Runnable>();
mThreadPool = new ThreadPoolExecutor(sNUMBER_OF_CORES, sNUMBER_OF_CORES, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, mTaskWorkQueue, new DefaultThreadFactory());
if (Version.hasGingerbread()) {
mThreadPool.allowCoreThreadTimeOut(true);
} else {
// Does nothing
}
}
/**
* The default thread factory
*/staticclass DefaultThreadFactory implements ThreadFactory {
privatestaticfinal AtomicInteger poolNumber = new AtomicInteger(1);
privatefinal ThreadGroup group;
privatefinal AtomicInteger threadNumber = new AtomicInteger(1);
privatefinal String namePrefix;
privatestaticfinal String sPre = "image-executor-pool-";
privatestaticfinal String sPost = "-thread-";
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = sPre + poolNumber.getAndIncrement() + sPost;
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
@Override
publicvoid execute(Runnable command) {
mThreadPool.execute(command);
}
@Override
publicvoid setTaskOrder(ImageTaskOrder order) {
mTaskWorkQueue.setTaskOrder(order);
}
}