/examples/android/app/src/main/java/com/tensorspeech/tensorflowtts/utils/ThreadPoolManager.java

https://github.com/dathudeptrai/TensorflowTTS · Java · 157 lines · 95 code · 20 blank · 42 comment · 8 complexity · c68baae1b725186053097536fb82dfee MD5 · raw file

  1. package com.tensorspeech.tensorflowtts.utils;
  2. import android.os.Looper;
  3. import android.os.Process;
  4. import java.util.concurrent.LinkedBlockingQueue;
  5. import java.util.concurrent.ScheduledThreadPoolExecutor;
  6. import java.util.concurrent.ThreadFactory;
  7. import java.util.concurrent.ThreadPoolExecutor;
  8. import java.util.concurrent.TimeUnit;
  9. import java.util.concurrent.atomic.AtomicInteger;
  10. /**
  11. * @author {@link "mailto:xuefeng.ding@outlook.com" "Xuefeng Ding"}
  12. * Created 2020-07-20 17:25
  13. */
  14. @SuppressWarnings("unused")
  15. public class ThreadPoolManager {
  16. public static ThreadPoolManager getInstance() {
  17. return ThreadPoolManager.Holder.INSTANCE;
  18. }
  19. private static final class Holder {
  20. private static final ThreadPoolManager INSTANCE = new ThreadPoolManager();
  21. }
  22. private ThreadPoolExecutor mExecutor;
  23. /**
  24. * Constructor
  25. */
  26. private ThreadPoolManager() {
  27. int corePoolSize = Runtime.getRuntime().availableProcessors() * 2 + 1;
  28. ThreadFactory namedThreadFactory = new NamedThreadFactory("thread pool");
  29. mExecutor = new ThreadPoolExecutor(
  30. corePoolSize,
  31. corePoolSize * 10,
  32. 1,
  33. TimeUnit.HOURS,
  34. new LinkedBlockingQueue<>(),
  35. namedThreadFactory,
  36. new ThreadPoolExecutor.DiscardPolicy()
  37. );
  38. }
  39. /**
  40. * 执行任务
  41. * @param runnable 需要执行的异步任务
  42. */
  43. public void execute(Runnable runnable) {
  44. if (runnable == null) {
  45. return;
  46. }
  47. mExecutor.execute(runnable);
  48. }
  49. /**
  50. * single thread with name
  51. * @param name 线程名
  52. * @return 线程执行器
  53. */
  54. public ScheduledThreadPoolExecutor getSingleExecutor(String name) {
  55. return getSingleExecutor(name, Thread.NORM_PRIORITY);
  56. }
  57. /**
  58. * single thread with name and priority
  59. * @param name thread name
  60. * @param priority thread priority
  61. * @return Thread Executor
  62. */
  63. @SuppressWarnings("WeakerAccess")
  64. public ScheduledThreadPoolExecutor getSingleExecutor(String name, int priority) {
  65. return new ScheduledThreadPoolExecutor(
  66. 1,
  67. new NamedThreadFactory(name, priority));
  68. }
  69. /**
  70. * 从线程池中移除任务
  71. * @param runnable 需要移除的异步任务
  72. */
  73. public void remove(Runnable runnable) {
  74. if (runnable == null) {
  75. return;
  76. }
  77. mExecutor.remove(runnable);
  78. }
  79. /**
  80. * 为线程池内的每个线程命名的工厂类
  81. */
  82. private static class NamedThreadFactory implements ThreadFactory {
  83. private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);
  84. private final ThreadGroup group;
  85. private final AtomicInteger threadNumber = new AtomicInteger(1);
  86. private final String namePrefix;
  87. private final int priority;
  88. /**
  89. * Constructor
  90. * @param namePrefix 线程名前缀
  91. */
  92. private NamedThreadFactory(String namePrefix) {
  93. this(namePrefix, Thread.NORM_PRIORITY);
  94. }
  95. /**
  96. * Constructor
  97. * @param threadName 线程名前缀
  98. * @param priority 线程优先级
  99. */
  100. private NamedThreadFactory(String threadName, int priority) {
  101. SecurityManager s = System.getSecurityManager();
  102. group = (s != null) ? s.getThreadGroup() :
  103. Thread.currentThread().getThreadGroup();
  104. namePrefix = threadName + "-" + POOL_NUMBER.getAndIncrement();
  105. this.priority = priority;
  106. }
  107. @Override
  108. public Thread newThread(Runnable r) {
  109. Thread t = new Thread(group, r,
  110. namePrefix + threadNumber.getAndIncrement(),
  111. 0);
  112. if (t.isDaemon()) {
  113. t.setDaemon(false);
  114. }
  115. t.setPriority(priority);
  116. switch (priority) {
  117. case Thread.MIN_PRIORITY:
  118. Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
  119. break;
  120. case Thread.MAX_PRIORITY:
  121. Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
  122. break;
  123. default:
  124. Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
  125. break;
  126. }
  127. return t;
  128. }
  129. }
  130. /**
  131. * 判断当前线程是否为主线程
  132. * @return {@code true} if the current thread is main thread.
  133. */
  134. public static boolean isMainThread() {
  135. return Looper.myLooper() == Looper.getMainLooper();
  136. }
  137. }