package cn.superdesk.uniorder.common.queue.block;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 这东西还不能投产 实验
 * @Author: linjinyu
 * @CreateDate: 2021/9/9 下午4:23
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/9/9 下午4:23
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
public class TaskScheduler {

	private final String TAG = "BlockingQueueTaskScheduler";

	private BlockTaskQueue mTaskQueue = BlockTaskQueue.getInstance();
	private TaskExecutor   mExecutor;

	private static class ShowDurationHolder {
		private final static TaskScheduler INSTANCE = new TaskScheduler();
	}

	private TaskScheduler() {
		initExecutor();
	}

	private void initExecutor() {
		mExecutor = new TaskExecutor(mTaskQueue);
		mExecutor.start();
	}

	public static TaskScheduler getInstance() {
		return ShowDurationHolder.INSTANCE;
	}

	public void enqueue(TaskInterface task) {


		// 因为TaskScheduler这里写成单例,如果isRunning改成false的话,不判断一下,就会一直都是false
		// 执行方是可以暂停的
		if (!mExecutor.isRunning()) {
			mExecutor.startRunning();
		}

		// 按照优先级插入队列 依次播放
		int taskQueueSize = mTaskQueue.add(task);

		log.info("按照优先级插入队列 依次播放(size={}) {}", taskQueueSize, task);
	}

	public void resetExecutor() {
		mExecutor.resetExecutor();
	}

	public void clearExecutor() {
		mExecutor.clearExecutor();
	}
}
