wjc
2024-12-23 f753d8366041354da60b8096060f3ab5159e3880
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.hdl.sdk.link.common.utils;
 
import android.os.Handler;
import android.os.Looper;
 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
 
/**
 * Created by Tong on 2021/9/15.
 */
public class ThreadToolUtils {
 
    private final Handler uiHandler = new Handler(Looper.getMainLooper());
 
    //cpu 最大线程容纳量
    private final int coreSize = Runtime.getRuntime().availableProcessors() + 1;
 
    private ThreadToolUtils() {
    }
 
    private static class SingletonInstance {
        private static final ThreadToolUtils INSTANCE = new ThreadToolUtils();
    }
 
    public static ThreadToolUtils getInstance() {
        return SingletonInstance.INSTANCE;
    }
 
 
    /**
     * 线程数量固定的线程池
     */
    public ExecutorService newFixedThreadPool(int size) {
        if (size == 0 || coreSize < size) {
            return Executors.newFixedThreadPool(coreSize);
        }
        return Executors.newFixedThreadPool(size);
    }
 
    /**
     * 定时任务线程池
     */
    public ScheduledExecutorService newScheduledThreadPool(int size) {
        if (size == 0 || coreSize < size) {
            return Executors.newScheduledThreadPool(coreSize);
        }
        return Executors.newScheduledThreadPool(size);
    }
 
    /**
     * 单一线程
     */
    public ExecutorService newSingleThreadPool() {
        return Executors.newSingleThreadExecutor();
    }
 
 
    public ExecutorService newCachedThreadPool() {
        return Executors.newCachedThreadPool();
    }
 
    /**
     * 切换回主线程
     */
    public void runOnUiThread(Runnable run) {
        uiHandler.post(run);
    }
 
 
}