用线程池的方案替换了rxjava

This commit is contained in:
tongchenfei
2020-07-09 16:20:59 +08:00
parent cc07e79efa
commit ecf6109cb6
4 changed files with 91 additions and 72 deletions

View File

@@ -7,7 +7,7 @@ import java.util.concurrent.ThreadFactory;
public class ThreadPoolService {
private static final ExecutorService SERVICE = Executors.newFixedThreadPool( 3, new ThreadFactoryImpl() );
private static final ExecutorService SINGLE_THREAD_SERVICE = Executors.newSingleThreadExecutor(new SingleThreadFactoryImpl());
private static class ThreadFactoryImpl implements ThreadFactory {
private static int mCounter = 1;
@@ -18,10 +18,26 @@ public class ThreadPoolService {
}
}
/**
* 单线程队列执行的ThreadFactory实现应该只会new一个Thread
*/
private static class SingleThreadFactoryImpl implements ThreadFactory{
private static int counter = 1;
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "SingleThread - " + counter++);
}
}
private ThreadPoolService() {
}
public static void execute( Runnable task ) {
SERVICE.execute( task );
}
public static void singleExecute(Runnable task) {
SINGLE_THREAD_SERVICE.execute(task);
}
}