package com.ardrawing.ardrawingspace.util;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class AppExecutors {
    private static AppExecutors instance;

    // 磁盘 I/O 线程池（单线程，用于数据库操作）
    private final Executor diskIO;

    // 主线程执行器（用于 UI 更新）
    private final Executor mainThread;

    // 网络线程池（可根据需要调整线程数）
    private final Executor networkIO;

    private AppExecutors() {
        this.diskIO = Executors.newSingleThreadExecutor();
        this.mainThread = new MainThreadExecutor();
        this.networkIO = Executors.newFixedThreadPool(3); // 3 个线程处理网络任务
    }

    public static AppExecutors getInstance() {
        if (instance == null) {
            synchronized (AppExecutors.class) {
                if (instance == null) {
                    instance = new AppExecutors();
                }
            }
        }
        return instance;
    }

    public Executor diskIO() {
        return diskIO;
    }

    public Executor mainThread() {
        return mainThread;
    }

    public Executor networkIO() {
        return networkIO;
    }

    private static class MainThreadExecutor implements Executor {
        private final Handler mainThreadHandler = new Handler(Looper.getMainLooper());

        @Override
        public void execute(Runnable command) {
            mainThreadHandler.post(command);
        }
    }
}
