线程池
简介
看到阿里巴巴 “开发军规” 里有这样的内容,所以针对此内容进行了整理。
样例
1 2 3 4 5
| <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>31.1-jre</version> </dependency>
|
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
| import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
public class ThreadPoolService {
private static final ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("TEST-POOL-%d").build();
private static final ExecutorService service = new ThreadPoolExecutor( 4, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(4), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy() );
public static ExecutorService getExecutorService() { return service; }
public static void newTask(Runnable r) { service.execute(r); }
public static void shutdown() { service.shutdown(); }
}
|
参考资料
阿里巴巴规范创建 Java 线程池