package com.sound.chat;

import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
import android.util.Log;
/**
 * 下载监控调度
 * @author Administrator
 */
public class DownloadTaskManagerThread implements Runnable {  
    
    public static final String TAG = "DownloadTaskManagerThread";

    private DownloadTaskManager downloadTaskManager;  
    // 创建一个可重用固定线程数的线程池  
    private ExecutorService pool;  
    // 线程池大小  
    private final int POOL_SIZE = 5;  
    // 轮询时间  
    private final int SLEEP_TIME = 1000;  
    // 是否停止  
    private boolean isStop = false;  
    //统计总的休眠时间
    private int total_sleep_time = 0; 
    
    public DownloadTaskManagerThread() {  
        downloadTaskManager = DownloadTaskManager.getInstance();  
        pool = Executors.newFixedThreadPool(POOL_SIZE);  
    }  
  
    @Override  
    public void run() {  
        while (!isStop) {  
            DownloadTask downloadTask = downloadTaskManager.getDownloadTask();  
            if (downloadTask != null) {
            	total_sleep_time = 0;
                pool.execute(downloadTask);  
            } else {  
                try {  
                    // 轮询  
                	if(total_sleep_time >= 10000){
                		this.isStop = true;
                	}
                	else{
                		total_sleep_time += SLEEP_TIME;
                		Thread.sleep(SLEEP_TIME);  
                	}
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
        if (isStop) {  
            pool.shutdown();  
        }  
    }  
  
    public boolean getStop(){
    	return this.isStop;
    }
}  