package com.alibaba.genie.panel.repository;
import android.os.Handler;
import android.os.Looper;
import com.alibaba.genie.panel.utils.thread.NameThreadFactory;
import com.aliyun.alink.linksdk.tools.ALog;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public final class ThreadPoolManager extends ThreadPoolExecutor {
public static final String TAG = "ThreadPoolManager";
private Handler mMainHandler;
private final Object mLock = new Object();
public ThreadPoolManager() {
super(0, 200, 30L, TimeUnit.MILLISECONDS,
new SynchronousQueue<>(), new NameThreadFactory("ThreadPoolManager"), new DiscardPolicy() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
super.rejectedExecution(r, e);
ALog.e(TAG, "Rejected Execution! pool size = " +
e.getPoolSize() + ", active threads = " + e.getActiveCount());
}
});
}
private static class SingleHolder {
private static final ThreadPoolManager INSTANCE = new ThreadPoolManager();
}
public static ThreadPoolManager getInstance() {
return SingleHolder.INSTANCE;
}
public void postToMainThread(Runnable runnable) {
if (this.mMainHandler == null) {
synchronized(this.mLock) {
if (this.mMainHandler == null) {
this.mMainHandler = new Handler(Looper.getMainLooper());
}
}
}
this.mMainHandler.post(runnable);
}
}