001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.utils; 017 018import java.util.concurrent.*; 019 020/** 021 * @author michael yang (fuhai999@gmail.com) 022 * @Date: 2019/11/22 023 */ 024public class NamedThreadPools { 025 026 public static ExecutorService newFixedThreadPool(String prefix) { 027 int nThreads = Runtime.getRuntime().availableProcessors(); 028 return newFixedThreadPool(nThreads, prefix); 029 } 030 031 032 public static ExecutorService newFixedThreadPool(int nThreads, String name) { 033 return Executors.newFixedThreadPool(nThreads, new NamedThreadFactory(name)); 034 } 035 036 037 public static ExecutorService newCachedThreadPool(String name) { 038 return newCachedThreadPool(new NamedThreadFactory(name)); 039 } 040 041 042 public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { 043 return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 044 60L, TimeUnit.SECONDS, 045 new SynchronousQueue<Runnable>(), 046 threadFactory); 047 } 048 049 050 public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, String name) { 051 return newScheduledThreadPool(corePoolSize, new NamedThreadFactory(name)); 052 } 053 054 055 public static ScheduledExecutorService newScheduledThreadPool( 056 int corePoolSize, ThreadFactory threadFactory) { 057 return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); 058 } 059}