package com.jqmobile.core.utils.thread;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.jqmobile.core.utils.thread.AsyncTask;
import com.jqmobile.core.utils.thread.IThreadPool;
import com.jqmobile.core.utils.thread.WaitingQueueToBigException;
public class BaseThreadPool extends Thread implements IThreadPool{
private int maxTaskCount = IThreadPool.DefaultWaitCount;
private final List<AsyncTask<?, ?>> tasks = Collections.synchronizedList(new ArrayList<AsyncTask<?,?>>());
private final List<BaseThread> threads = new ArrayList<BaseThread>();
private final int maxThreadCount;
private final long maxFreeTime;
private final Object lock = new Object();
/**
* @param initThreadCount 初始化线程数
* @param maxThreadCount 最大线程数
* @param maxFreeTime 最长线程空闲时间
*/
public BaseThreadPool(int initThreadCount, int maxThreadCount, long maxFreeTime){
for(int i=0; i<initThreadCount; i++){
createThread(maxFreeTime);
}
this.maxFreeTime = maxFreeTime;
this.maxThreadCount = maxThreadCount;
}
private void createThread(long maxFreeTime) {
if(getThreadCount()<maxThreadCount){
BaseThread t = new BaseThread(maxFreeTime);
threads.add(t);
t.start();
}
}
@Override
public void run() {
while (true) {
synchronized (lock) {
try {
lock.wait(100000);
} catch (InterruptedException e) {
e.printStackTrace();
continue;
}
}
}
}
/**
* @param initThreadCount 初始化线程数
* @param maxThreadCount 最大线程数
* @param maxFreeTime 最长线程空闲时间
*/
public BaseThreadPool(int maxThreadCount, long maxFreeTime){
this(maxThreadCount/10, maxThreadCount, maxFreeTime);
}
@Override
public IThreadPool putTask(AsyncTask<?, ?> task)
throws WaitingQueueToBigException {
if(getTaskCount()>=maxTaskCount){
throw new WaitingQueueToBigException();
}
task.onPreExecute();
synchronized (tasks) {
tasks.add(task);
tasks.notify();
autoThread();
}
return this;
}
private void autoThread() {
if(getTaskCount()>getThreadCount()){
createThread(maxFreeTime);
}
}
@Override
public int getTaskCount() {
return tasks.size();
}
@Override
public int getThreadCount() {
return threads.size();
}
@Override
public void setMaxWaitingQueue(int maxWaitingCount) {
this.maxTaskCount = maxWaitingCount;
}
void remove(AsyncTask<?, ?> task){
tasks.remove(task);
}
//
class BaseThread extends Thread{
private long waitTime;
private boolean busy = false;
private boolean run = true;
public BaseThread(long waitTime){
this.waitTime = waitTime;
}
@SuppressWarnings("unchecked")
@Override
public void run() {
try{
boolean one = true;
while(run & !interrupted()){
AsyncTask<Object, Object> task;
synchronized (tasks) {
if(tasks.size() == 0){
try {
tasks.wait(waitTime);
} catch (InterruptedException e) {
// e.printStackTrace();
}
}
if(tasks.size()>0){
one = true;
task = (AsyncTask<Object, Object>) tasks.remove(0);
}else{
if(one){
one = false;
continue;
}else{
try{
return;
}finally{
this.setRun(false);
threads.remove(this);
this.interrupt();
}
}
}
}
if(null == task){
continue;
}
try{
busy=true;
task.status = AsyncTask.Status.RUNNING;
task.setResult(task.doInBackground(task.getParams()));
task.onPostExecute(task.getResult());
}catch(Throwable e){
task.onException(e);
}finally{
task.status = AsyncTask.Status.FINISHED;
// System.out.println(task);
task=null;
busy=false;
}
}
}catch(Throwable e){
run();
}
}
public boolean isBusy() {
return busy;
}
public void setRun(boolean run) {
this.run = run;
}
}
}