¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.mandi.fendan.util; |
| | | |
| | | import javax.validation.constraints.NotNull; |
| | | import java.util.concurrent.LinkedBlockingQueue; |
| | | import java.util.concurrent.ThreadFactory; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | /** |
| | | * 线ç¨æ± çå·¥å
·ç±» |
| | | * ç¨äºè¿è¡çº¿ç¨ç管çï¼é²æ¢éå¤å建ãææ»çº¿ç¨ã |
| | | * |
| | | |
| | | * å¤çº¿ç¨è¿è¡æé´ï¼å¦æç³»ç»ä¸æçå建ãææ»æ°çº¿ç¨ï¼ |
| | | * ä¼äº§çè¿åº¦æ¶èç³»ç»èµæºï¼ä»¥åè¿åº¦åæ¢çº¿ç¨çé®é¢ï¼çè³å¯è½å¯¼è´ç³»ç»èµæºçå´©æºã |
| | | * å æ¤éè¦çº¿ç¨æ± ï¼å¯¹çº¿ç¨è¿è¡ç®¡çã |
| | | */ |
| | | public class ThreadPoolUtil { |
| | | |
| | | private String TAG = getClass().getName(); |
| | | private static volatile ThreadPoolUtil mInstance; |
| | | //æ ¸å¿çº¿ç¨æ± çæ°éï¼åæ¶è½å¤æ§è¡ç线ç¨æ°é |
| | | private int corePoolSize; |
| | | //æ大线ç¨æ± æ°éï¼è¡¨ç¤ºå½ç¼å²éå满çæ¶åè½ç»§ç»å®¹çº³ççå¾
ä»»å¡çæ°é |
| | | private int maxPoolSize; |
| | | //åæ´»æ¶é´ |
| | | private long keepAliveTime = 1; |
| | | private TimeUnit unit = TimeUnit.HOURS; |
| | | private ThreadPoolExecutor executor; |
| | | |
| | | private ThreadPoolUtil() { |
| | | //ç»corePoolSizeèµå¼ï¼å½å设å¤å¯ç¨å¤çå¨æ ¸å¿æ°*2 + 1,è½å¤è®©cpuçæçå¾å°æ大ç¨åº¦æ§è¡ï¼æç 究论è¯çï¼ |
| | | corePoolSize = Runtime.getRuntime().availableProcessors() * 2 + 1; |
| | | maxPoolSize = corePoolSize; |
| | | executor = new ThreadPoolExecutor( |
| | | //å½æä¸ªæ ¸å¿ä»»å¡æ§è¡å®æ¯ï¼ä¼ä¾æ¬¡ä»ç¼å²éåä¸ååºçå¾
ä»»å¡ |
| | | corePoolSize, |
| | | // ç¶ånew LinkedBlockingQueue(),ç¶åmaximumPoolSize,ä½æ¯å®çæ°éæ¯å
å«äºcorePoolSizeç |
| | | maxPoolSize, |
| | | //表示çæ¯maximumPoolSizeå½ä¸çå¾
ä»»å¡çåæ´»æ¶é´ |
| | | keepAliveTime, |
| | | unit, |
| | | //ç¼å²éåï¼ç¨äºåæ¾çå¾
ä»»å¡ï¼Linkedçå
è¿å
åº |
| | | new LinkedBlockingQueue(), |
| | | new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"), |
| | | new ThreadPoolExecutor.AbortPolicy() |
| | | ); |
| | | } |
| | | |
| | | public static ThreadPoolUtil getInstance() { |
| | | if (mInstance == null) { |
| | | synchronized (ThreadPoolUtil.class) { |
| | | if (mInstance == null) { |
| | | mInstance = new ThreadPoolUtil(); |
| | | } |
| | | } |
| | | } |
| | | return mInstance; |
| | | } |
| | | |
| | | /** |
| | | * æ§è¡ä»»å¡ |
| | | * |
| | | * @param runnable |
| | | */ |
| | | public void execute(Runnable runnable) { |
| | | if (executor == null) { |
| | | executor = new ThreadPoolExecutor( |
| | | corePoolSize, |
| | | maxPoolSize, |
| | | keepAliveTime, |
| | | TimeUnit.SECONDS, |
| | | new LinkedBlockingQueue(), |
| | | new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"), |
| | | new ThreadPoolExecutor.AbortPolicy()); |
| | | } |
| | | if (runnable != null) { |
| | | executor.execute(runnable); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 移é¤ä»»å¡ |
| | | * |
| | | * @param runnable |
| | | */ |
| | | public void remove(Runnable runnable) { |
| | | if (runnable != null) { |
| | | executor.remove(runnable); |
| | | } |
| | | } |
| | | |
| | | private static class DefaultThreadFactory implements ThreadFactory { |
| | | //线ç¨æ± çè®¡æ° |
| | | private static final AtomicInteger poolNumber = new AtomicInteger(1); |
| | | //线ç¨çè®¡æ° |
| | | private final AtomicInteger threadNumber = new AtomicInteger(1); |
| | | private final ThreadGroup group; |
| | | private final String namePrefix; |
| | | private final int threadPriority; |
| | | |
| | | DefaultThreadFactory(int threadPriority, String threadNamePrefix) { |
| | | this.threadPriority = threadPriority; |
| | | this.group = Thread.currentThread().getThreadGroup(); |
| | | this.namePrefix = threadNamePrefix + poolNumber.getAndIncrement() + "-thread-"; |
| | | } |
| | | |
| | | @Override |
| | | public Thread newThread(@NotNull Runnable r) { |
| | | Thread thread = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); |
| | | // è¿åTrue该线ç¨å°±æ¯å®æ¤çº¿ç¨ |
| | | // å®æ¤çº¿ç¨åºè¯¥æ°¸è¿ä¸å»è®¿é®åºæèµæºï¼å¦ï¼æ°æ®åºãæ件çãå 为å®ä¼å¨ä»»ä½æ¶åçè³å¨ä¸ä¸ªæä½çä¸é´åçä¸æã |
| | | if (thread.isDaemon()) { |
| | | thread.setDaemon(false); |
| | | } |
| | | thread.setPriority(threadPriority); |
| | | return thread; |
| | | } |
| | | } |
| | | } |