Files
MoGoEagleEye/foudations/mogo-utils/src/main/java/com/mogo/utils/ThreadPoolService.java
2020-07-09 16:20:59 +08:00

43 lines
1.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.mogo.utils;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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;
@Override
public Thread newThread( Runnable r ) {
return new Thread( r, "ThreadPoolService - " + mCounter++ );
}
}
/**
* 单线程队列执行的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);
}
}