我是靠谱客的博主 长情镜子,这篇文章主要介绍android:线程工具类,现在分享给大家,希望可以做个参考。

复制代码
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
public class ThreadPoolUtils { //设备cpu核数 private final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors(); //线程池最大线程数 private final int MAXIMUM_POOL_SIZE = 16; //空闲线程存活时间 private static final int KEEP_ALIVE_SECONDS =3; //io型线程设置 private static final ThreadFactory ioThreadFactory = new ThreadFactory() { private final AtomicInteger mCount = new AtomicInteger(1); @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r,"IO #" +mCount.getAndIncrement()); Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); return thread; } }; //cpu计算型线程设置 private static final ThreadFactory cpuThreadFactory = new ThreadFactory() { private final AtomicInteger mCount = new AtomicInteger(1); @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r,"CPU #" +mCount.getAndIncrement()); Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); return thread; } }; //io与cpu混合型 private static final ThreadFactory miscThreadFactory = new ThreadFactory() { private final AtomicInteger mCount = new AtomicInteger(1); @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r,"MISC #" +mCount.getAndIncrement()); Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); return thread; } }; public Executor ioExecutor = new ThreadPoolExecutor(MAXIMUM_POOL_SIZE,MAXIMUM_POOL_SIZE,KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(),ioThreadFactory); public Executor cpuExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,CORE_POOL_SIZE,KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(),cpuThreadFactory); public Executor miscExecutor = Executors.newFixedThreadPool(3,miscThreadFactory); public Executor getIoExecutor() { return ioExecutor; } public Executor getCpuExecutor() { return cpuExecutor; } public Executor getMiscExecutor() { return miscExecutor; } public void ioExecute(Runnable runnable){ ioExecutor.execute(runnable);} }

最后

以上就是长情镜子最近收集整理的关于android:线程工具类的全部内容,更多相关android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部