package com.zhiwei.manage.handle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.zhiwei.manage.util.ThreadPool;
import com.zhiwei.manage.util.Tools;

@Component
public class QueueTask extends Thread {

	private static final Log log=LogFactory.getLog(QueueTask.class);
	private ThreadPool t = ThreadPool.getThreadPool(30);
	private static boolean isRun = true;

	public QueueTask() {
		this.start();
	}

	public void stopMe() {
		isRun = false;
	}

	@Override
	public void run() {
		log.info("队列线程启动");
		while (isRun) {
			if (FieldThread.queue.size() > 0 ) {
				t.execute(new Task(FieldThread.queue.poll()));
			} else {
				Tools.sleep(1000);
			}
			if (PingThread.queue.size() > 0 ) {
				t.execute(new Task(PingThread.queue.poll()));
			} else {
				Tools.sleep(1000);
			}
			if (DelayedThread.queue.size() > 0 ) {
				t.execute(new Task(DelayedThread.queue.poll()));
			} else {
				Tools.sleep(1000);
			}
			if (FieldThread.queue.size() == 0 && PingThread.queue.size() == 0 && DelayedThread.queue.size() == 0) {
				Tools.sleep(1000 * 10);
			}
		}
	}
}
