package com.gnhz.clinic.eitc_erm_app.sightGlass;

import android.util.Log;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

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

public class ThreadPoolHelper {
    /*线程池大小*/
    private static final int POOL_SIZE = 10;
    private static final String TAG = "czb";

    //订单任务线程池

    private static ThreadPoolExecutor comitTaskPool = (ThreadPoolExecutor) new ScheduledThreadPoolExecutor(POOL_SIZE,
            new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());


    /**
     * 执行订单任务
     *
     * @param comitTask
     */
    public static void executeTask(Runnable comitTask) {
        comitTaskPool.execute(comitTask);
        Log.e("debug", "【线程池任务】线程池中线程数：" + comitTaskPool.getPoolSize());
        Log.e("debug", "【线程池任务】队列中等待执行的任务数：" + comitTaskPool.getQueue().size());
        // Log.e("debug","【线程池任务】已执行完任务数：" + comitTaskPool.getCompletedTaskCount());
    }


    /**
     * 关闭线程池
     */
    public static void shutdown() {
        Log.e(TAG, "shutdown comitTaskPool...");
        comitTaskPool.shutdown();
        try {
            if (!comitTaskPool.isTerminated()) {
                Log.e(TAG, "shutdown: 直接关闭失败[" + comitTaskPool.toString() + "]");

                comitTaskPool.awaitTermination(3, TimeUnit.SECONDS);
                if (comitTaskPool.isTerminated()) {
                    Log.e(TAG, "shutdown: 成功关闭[" + comitTaskPool.toString() + "]");

                } else {
                    Log.e(TAG, "[" + comitTaskPool.toString() + "]关闭失败，执行shutdownNow...");
                    if (comitTaskPool.shutdownNow().size() > 0) {
                        Log.e(TAG, "[" + comitTaskPool.toString() + "]没有关闭成功");
                    } else {
                        Log.e(TAG, "shutdown: shutdownNow执行完毕，成功关闭[" + comitTaskPool.toString() + "]");

                    }
                }
            } else {
                Log.e(TAG, "shutdown: 成功关闭[" + comitTaskPool.toString() + "]");

            }
        } catch (InterruptedException e) {
            Log.e(TAG, "shutdown: 接收到中断请" + comitTaskPool.toString() + "停止操作");

        }
    }

}