package com.sunzee.thread.homepageA.homepage;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 长时间或者推送要执行的线程池
 */
public class ThreadPoolHomepageA {
    private ScheduledThreadPoolExecutor scheduled;

    private ScheduledFuture<?>
            mScheduledFutureD169,
            mScheduledFutureD170, mScheduledFutureD172;
    private HomepageATask
            mTaskRunD169,
            mTaskRunD170, mTaskRunD172;

    public ThreadPoolHomepageA() {
        scheduled = new ScheduledThreadPoolExecutor(3);
        //设置线程池的策略  抛弃旧的任务
        scheduled.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
    }

    public void startAll(){
        mTaskRunD169 = new HomepageATask(HomepageAThreadType.runD169);
        mScheduledFutureD169 = scheduled.scheduleAtFixedRate(mTaskRunD169, 0, 1000, TimeUnit.MILLISECONDS);

        mTaskRunD170 = new HomepageATask(HomepageAThreadType.runD170);
        mScheduledFutureD170 = scheduled.scheduleAtFixedRate(mTaskRunD170, 0, 1000, TimeUnit.MILLISECONDS);

        mTaskRunD172 = new HomepageATask(HomepageAThreadType.runD172);
        mScheduledFutureD172 = scheduled.scheduleAtFixedRate(mTaskRunD172, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public static void stopRead(HomepageATask threadTask, ScheduledFuture<?> scheduledFuture1) {
        if (threadTask != null) {
            threadTask.setIsRun(false);
        }
        if (scheduledFuture1 != null) {
            scheduledFuture1.cancel(true);
        }
    }

    public void stopAll() {
        stopRead(mTaskRunD169,mScheduledFutureD169);
        stopRead(mTaskRunD170,mScheduledFutureD170);
        stopRead(mTaskRunD172,mScheduledFutureD172);

    }

    public void stopALLRead() {
        stopAll();
    }

}
