package com.player.smallBear.utils;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.CallSuper;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/05/08
* desc : utils about thread
* </pre>
*/
public final class ThreadUtils {
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
private static final Map<Integer, Map<Integer, ExecutorService>> TYPE_PRIORITY_POOLS = new HashMap<>();
private static final Map<Task, ExecutorService> TASK_POOL_MAP = new ConcurrentHashMap<>();
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final Timer TIMER = new Timer();
private static final byte TYPE_SINGLE = -1;
private static final byte TYPE_CACHED = -2;
private static final byte TYPE_IO = -4;
private static final byte TYPE_CPU = -8;
private static Executor sDeliver;
/**
* Return whether the thread is the main thread.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isMainThread() {
return Looper.myLooper() == Looper.getMainLooper();
}
public static Handler getMainHandler() {
return HANDLER;
}
public static void runOnUiThread(final Runnable runnable) {
if (Looper.myLooper() == Looper.getMainLooper()) {
runnable.run();
} else {
HANDLER.post(runnable);
}
}
public static void runOnUiThreadDelayed(final Runnable runnable, long delayMillis) {
HANDLER.postDelayed(runnable, delayMillis);
}
/**
* Return a thread pool that reuses a fixed number of threads
* operating off a shared unbounded queue, using the provided
* ThreadFactory to create new threads when needed.
*
* @param size The size of thread in the pool.
* @return a fixed thread pool
*/
public static ExecutorService getFixedPool(@IntRange(from = 1) final int size) {
return getPoolByTypeAndPriority(size);
}
/**
* Return a thread pool that reuses a fixed number of threads
* operating off a shared unbounded queue, using the provided
* ThreadFactory to create new threads when needed.
*
* @param size The size of thread in the pool.
* @param priority The priority of thread in the poll.
* @return a fixed thread pool
*/
public static ExecutorService getFixedPool(@IntRange(from = 1) final int size,
@IntRange(from = 1, to = 10) final int priority) {
return getPoolByTypeAndPriority(size, priority);
}
/**
* Return a thread pool that uses a single worker thread operating
* off an unbounded queue, and uses the provided ThreadFactory to
* create a new thread when needed.
*
* @return a single thread pool
*/
public static ExecutorService getSinglePool() {
return getPoolByTypeAndPriority(TYPE_SINGLE);
}
/**
* Return a thread pool that uses a single worker thread operating
* off an unbounded queue, and uses the provided ThreadFactory to
* create a new thread when needed.
*
* @param priority The priority of thread in the poll.
* @return a single thread pool
*/
public static ExecutorService getSinglePool(@IntRange(from = 1, to = 10) final int priority) {
return getPoolByTypeAndPriority(TYPE_SINGLE, priority);
}
/**
* Return a thread pool that creates new threads as needed, but
* will reuse previously constructed threads when they are
* available.
*
* @return a cached thread pool
*/
public static ExecutorService getCachedPool() {
return getPoolByTypeAndPriority(TYPE_CACHED);
}
/**
* Return a thread pool that creates new threads as needed, but
* will reuse previously constructed threads when they are
* available.
*
* @param priority The priority of thread in the poll.
* @return a cached thread pool
*/
public static ExecutorService getCachedPool(@IntRange(from = 1, to = 10) final int priority) {
return getPoolByTypeAndPriority(TYPE_CACHED, priority);
}
/**
* Return a thread pool that creates (2 * CPU_COUNT + 1) threads
* operating off a queue which size is 128.
*
* @return a IO thread pool
*/
public static ExecutorService getIoPool() {
return getPoolByTypeAndPriority(TYPE_IO);
}
/**
* Return a thread pool that creates (2 * CPU_COUNT + 1) threads
* operating off a queue which size is 128.
*
* @param priority The priority of thread in the poll.
* @return a IO thread pool
*/
public static ExecutorService getIoPool(@IntRange(from = 1, to = 10) final int priority) {
return getPoolByTypeAndPriority(TYPE_IO, priority);
}
/**
* Return a thread pool that creates (CPU_COUNT + 1) threads
* operating off a queue which size is 128 and the maximum
* number of threads equals (2 * CPU_COUNT + 1).
*
* @return a cpu thread pool for
*/
public static ExecutorService getCpuPool() {
return getPoolByTypeAndPriority(TYPE_CPU);
}
/**
* Return a thread pool that creates (CPU_COUNT + 1) threads
* operating off a queue which size is 128 and the maximum
* number of threads equals (2 * CPU_COUNT + 1).
*
* @param priority The priority of thread in the poll.
* @return a cpu thread pool for
*/
public static ExecutorService getCpuPool(@IntRange(from = 1, to = 10) final int priority) {
return getPoolByTypeAndPriority(TYPE_CPU, priority);
}
/**
* Executes the given task in a fixed thread pool.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param <T> The type of the task's result.
*/
public static <T> void executeByFixed(@IntRange(from = 1) final int size, final Task<T> task) {
execute(getPoolByTypeAndPriority(size), task);
}
/**
* Executes the given task in a fixed thread pool.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param priority The priority of thread in the poll.
* @param <T> The type of the task's result.
*/
public static <T> void executeByFixed(@IntRange(from = 1) final int size,
final Task<T> task,
@IntRange(from = 1, to = 10) final int priority) {
execute(getPoolByTypeAndPriority(size, priority), task);
}
/**
* Executes the given task in a fixed thread pool after the given delay.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param <T> The type of the task's result.
*/
public static <T> void executeByFixedWithDelay(@IntRange(from = 1) final int size,
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Java实现的小熊播放器设计源码 (310个子文件)
gradlew.bat 3KB
app-release.dm 5KB
app-release.dm 5KB
.gitignore 225B
.gitignore 47B
.gitignore 6B
build.gradle 6KB
settings.gradle 1KB
build.gradle 66B
gradlew 6KB
gradle-wrapper.jar 58KB
ThreadUtils.java 53KB
AppHelper.java 38KB
HttpUtils.java 28KB
ACache.java 27KB
CustomActionBar.java 19KB
LoginPresenter.java 15KB
PlayOnlineVideoActivity.java 12KB
BindDeviceForm.java 11KB
GPApplication.java 10KB
BrowserActivity.java 9KB
StatusBarUtil.java 9KB
SPDanmakuView.java 9KB
CourseDetailsPresenter.java 8KB
StandardVideoController.java 8KB
RealNamePresenter.java 8KB
RealNameAuthActivity.java 7KB
FeedbackPresenter.java 7KB
BaseActivity.java 7KB
JsonCallback.java 6KB
BaseFragment.java 6KB
JsonConvert.java 6KB
FeedbackActivity.java 6KB
CommendInfo.java 5KB
VideosOfflineFragment.java 5KB
ClearEditText.java 5KB
LoginConfigInfo.java 5KB
LoginActivity.java 5KB
Common.java 5KB
PicassoUtils.java 5KB
FormatParser.java 5KB
GuideActivity.java 4KB
TextCallBack.java 4KB
VideosOnlineFragment.java 4KB
MinePagePresenter.java 4KB
PictureSelectorHelper.java 4KB
AppStartActivity.java 4KB
CourseInfo.java 4KB
SmallPlayerControlView.java 4KB
PortraitWhenFullScreenController.java 4KB
SonicJavaScriptInterface.java 4KB
MineFragment.java 4KB
CourseOfflinePresenter.java 4KB
AboutInfo.java 3KB
UserInfo.java 3KB
MainActivity.java 3KB
SonicRuntimeImpl.java 3KB
VideosCommentListFragment.java 3KB
LibCollections.java 3KB
VideosDetailListFragment.java 3KB
VideosPersonalNotesFragment.java 3KB
CustomProgressDialog.java 3KB
GlideEngine.java 3KB
AboutActivity.java 3KB
RomUtils.java 3KB
StudentSettingInfo.java 3KB
BasePresenter.java 3KB
MerchantInfo.java 3KB
GsonUtils.java 3KB
RecyclerItemDecoration.java 3KB
AboutPresenter.java 3KB
DeviceManageLoginDevicePresenter.java 3KB
CourseOnlinePresenter.java 2KB
DeviceManageActivity.java 2KB
FirstCourseInfoAdapter.java 2KB
MeController.java 2KB
AboutUsPresenter.java 2KB
CourseEntity.java 2KB
FaceSdkSign.java 2KB
GlobalUtils.java 2KB
BaseEntity.java 2KB
GlobalVariables.java 2KB
LyStatusBar.java 2KB
BaseDialogFragment.java 2KB
AboutUsActivity.java 2KB
AppDatabase.java 2KB
ProportionChoicePopupView.java 2KB
ResultData.java 2KB
PlayVideoActivity.java 2KB
XYSPUtils.java 2KB
FeedbackAdapter.java 2KB
FileUtils.java 2KB
MultipleChoicePopupView.java 2KB
ImageFileCompressEngine.java 2KB
Convert.java 2KB
CourseController.java 2KB
BottomAddLocalVideosDialog.java 2KB
BlurTransformation.java 1KB
BottomOrderTypeDialog.java 1KB
CenteredImageSpan.java 1KB
共 310 条
- 1
- 2
- 3
- 4
资源评论
xyq2024
- 粉丝: 2965
- 资源: 5572
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 后勤管理处服务工作标准.docx
- 后勤维修材料管理制度.docx
- 寒假作息时间表.docx
- 基层应急救援站建设提升基层安全防范和应急救援能力的实施方案.docx
- 磷酸铁、磷酸铁锂生产工艺流程.docx
- 环境问题调查表.docx
- 普通高等学校运动训练、武术与民族传统体育专业招生管理办法.docx
- 设备管理制度(全).docx
- 乳酸阈值对照表.docx
- 神经肌肉中心皮肌炎诊断标准.docx
- 硕士研究生招生考试(初试)《考场规则》.docx
- 消防安全常识二十条.docx
- 学校交通安全知识培训内容.docx
- 医疗废物处置管理制度.docx
- 医疗卫生机构医疗废物管理制度.docx
- 招投标中标后,招标方的操作流程.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功