package com.sunzee.thread.myservice;

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

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

    private ScheduledFuture<?>
            mScheduledFutureM600, mScheduledFutureM8, mScheduledFutureM17,
            mScheduledFutureSet, mScheduledFutureRst, mScheduledFutureWrite;
    private MyserviceTask
            mTaskRunM600, MTaskRunM8, MTaskRunM17,
            mTaskRunMRst, mTaskRunMSet, mTaskRunWrite;

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

    public void startALLRead() {
        mTaskRunM600 = new MyserviceTask(MyserviceThreadType.runM600);
        mScheduledFutureM600 = scheduled.scheduleAtFixedRate(mTaskRunM600, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void startSetM8() {
        MTaskRunM8 = new MyserviceTask(MyserviceThreadType.runM8);
        mScheduledFutureM8 = scheduled.scheduleAtFixedRate(MTaskRunM8, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void startSetM17() {
        MTaskRunM17 = new MyserviceTask(MyserviceThreadType.runM17);
        mScheduledFutureM17 = scheduled.scheduleAtFixedRate(MTaskRunM17, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void startRst(String address) {
        mTaskRunMRst = new MyserviceTask(MyserviceThreadType.runRst, address);
        mScheduledFutureRst = scheduled.scheduleAtFixedRate(mTaskRunMRst, 0, 1000, TimeUnit.MILLISECONDS);
    }


    public void startSet(String address) {
        mTaskRunMSet = new MyserviceTask(MyserviceThreadType.runSet, address);
        mScheduledFutureSet = scheduled.scheduleAtFixedRate(mTaskRunMSet, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void startWrite(String paraAddress, String data) {
        mTaskRunWrite = new MyserviceTask(MyserviceThreadType.runWrite, paraAddress, data);
        mScheduledFutureWrite = scheduled.scheduleAtFixedRate(mTaskRunWrite, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void stopWrite() {
        stopRead(mTaskRunWrite, mScheduledFutureWrite);
    }

    public void stopRst() {
        stopRead(mTaskRunMRst, mScheduledFutureRst);
    }

    public void stopSet() {
        stopRead(mTaskRunMSet, mScheduledFutureSet);
    }


    public void stopReadM600() {
        stopRead(mTaskRunM600, mScheduledFutureM600);
    }


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

    public void stopSetM17() {
        stopRead(MTaskRunM17, mScheduledFutureM17);
    }

    public void stopSetM8() {
        stopRead(MTaskRunM8, mScheduledFutureM8);
    }

    public void stopALLRead(){
        stopWrite();
        stopRst();
        stopSet();
        stopReadM600();
        stopSetM8();
        stopSetM17();
    }

}
