me.andy5.util.concurrent.PriorityThreadPoolExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of priority-thread-pool-executor Show documentation
Show all versions of priority-thread-pool-executor Show documentation
A java thread pool with assigned and dynamically adjusted task priority
The newest version!
package me.andy5.util.concurrent;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 一个具有指定和动态调整任务优先级能力的Java线程池
* A java thread pool with the ability to specify and dynamically adjust task priorities
*
* @author andy(Andy)
* @datetime 2019-09-19 09:01 GMT+8
* @email [email protected]
*/
public class PriorityThreadPoolExecutor extends ThreadPoolExecutor {
// private static Log log = Log.getLog(PriorityThreadPoolExecutor.class);
public PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
PriorityBlockingQueue workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
public PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
PriorityBlockingQueue workQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
public PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
PriorityBlockingQueue workQueue, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
}
public PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
PriorityBlockingQueue workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
/**
* 指定优先级执行Runnable
* Specify priority to execute Runnable
*
* @param command
* @param priority
* @return
*/
public PriorityRunnable execute(Runnable command, int priority) {
if (command instanceof PriorityRunnable) {
this.execute(command);
return (PriorityRunnable) command;
}
PriorityRunnable runnable = new PriorityRunnableAdapter(new DefaultPriority(priority), command);
this.execute(runnable);
return runnable;
}
/**
* 指定优先级执行Runnable
* Specify priority to execute Runnable
*
* @param task
* @param priority
* @return
*/
public PriorityFuture> submit(Runnable task, int priority) {
if (task instanceof Priority) {
return (PriorityFuture>) this.submit(task);
}
return (PriorityFuture>) this.submit(new PriorityRunnableAdapter(new DefaultPriority(priority), task));
}
/**
* 指定优先级执行Runnable
* Specify priority to execute Runnable
*
* @param task
* @param result
* @param priority
* @param
* @return
*/
public PriorityFuture submit(Runnable task, T result, int priority) {
if (task instanceof Priority) {
return (PriorityFuture) this.submit(task, result);
}
return (PriorityFuture) this.submit(new PriorityRunnableAdapter(new DefaultPriority(priority), task),
result);
}
/**
* 指定优先级执行Callable
* Specify priority to execute Callable
*
* @param task
* @param priority
* @param
* @return
*/
public PriorityFuture submit(Callable task, int priority) {
if (task instanceof Priority) {
return (PriorityFuture) this.submit(task);
}
return (PriorityFuture) this.submit(new PriorityCallableAdapter(new DefaultPriority(priority), task));
}
@Override
protected RunnableFuture newTaskFor(Callable callable) {
return new PriorityFutureAdapter(callable);
}
@Override
protected RunnableFuture newTaskFor(Runnable runnable, T value) {
return new PriorityFutureAdapter(runnable, value);
}
/**
* 传递子类PriorityRunnable,传递Runnable而非PriorityRunnable的话,将不支持优先级调整
* 如果要使用Runnable又需要支持优先级可用扩展方法{{@link #execute(Runnable, int)}}并使用其返回值进行优先级调整
*
* Use the subclass PriorityRunnable instead of Runnable, use Runnable instead of PriorityRunnable will not support priority adjustment
* if it is need to use Runnable support priority you can use the extension methods {{@link #execute(Runnable, int)}} and use its return value to adjust the priority
*
* @param command
*/
@Override
public void execute(Runnable command) {
// 防止重复包装
// Avoid duplicate packaging
if (command instanceof PriorityComparable) {
super.execute(command);
return;
}
// 使用适配器将实现了Priority接口的Runnable进行适配
// Use the adapter to adapt Runnable that implements the Priority interface
if (command instanceof Priority) {
super.execute(new PriorityRunnableAdapter((Priority) command, command));
return;
}
super.execute(new PriorityRunnableAdapter(new DefaultPriority(), command));
}
/**
* 传递子类PriorityRunnable,传递Runnable而非PriorityRunnable的话,将不支持优先级调整
* 如果要使用Runnable又需要支持优先级可用扩展方法{{@link #submit(Runnable, int)}}并使用其返回值进行优先级调整
*
* Use the subclass PriorityRunnable instead of Runnable, use Runnable instead of PriorityRunnable will not support priority adjustment
* if it is need to use Runnable support priority you can use the extension methods {{@link #submit(Runnable, int)}} and use its return value to adjust the priority
*
* @param task
* @return
*/
@Override
public Future> submit(Runnable task) {
// 防止重复包装
// Avoid duplicate packaging
if (task instanceof PriorityComparable) {
return super.submit(task);
}
// 使用适配器将实现了Priority接口的Runnable进行适配
// Use the adapter to adapt Runnable that implements the Priority interface
if (task instanceof Priority) {
return super.submit(new PriorityRunnableAdapter((Priority) task, task));
}
return super.submit(new PriorityRunnableAdapter(new DefaultPriority(), task));
}
/**
* 传递子类PriorityRunnable,传递Runnable而非PriorityRunnable的话,将不支持优先级调整
* 如果要使用Runnable又需要支持优先级可用扩展方法{{@link #submit(Runnable, Object, int)}}并使用其返回值进行优先级调整
*
* Use the subclass PriorityRunnable instead of Runnable, use Runnable instead of PriorityRunnable will not support priority adjustment
* if it is need to use Runnable support priority you can use the extension methods {{@link #submit(Runnable, Object, int)}} and use its return value to adjust the priority
*
* @param task
* @param result
* @param
* @return
*/
@Override
public Future submit(Runnable task, T result) {
// 防止重复包装
// Avoid duplicate packaging
if (task instanceof PriorityComparable) {
return super.submit(task, result);
}
// 使用适配器将实现了Priority接口的Runnable进行适配
// Use the adapter to adapt Runnable that implements the Priority interface
if (task instanceof Priority) {
return super.submit(new PriorityRunnableAdapter((Priority) task, task), result);
}
return super.submit(new PriorityRunnableAdapter(new DefaultPriority(), task), result);
}
/**
* 传递子类PriorityCallable,传递Callable而非PriorityCallable的话,将不支持优先级调整
* 如果要使用Callable又需要支持优先级可用扩展方法{{@link #submit(Callable, int)}}并使用其返回值进行优先级调整
*
* Use the subclass PriorityCallable instead of Callable, use Callable instead of PriorityCallable will not support priority adjustment
* if it is need to use Callable support priority you can use the extension methods {{@link #submit(Callable, int)}} and use its return value to adjust the priority
*
* @param task
* @param
* @return
*/
@Override
public Future submit(Callable task) {
// 防止重复包装
// Avoid duplicate packaging
if (task instanceof PriorityComparable) {
return super.submit(task);
}
// 使用适配器将实现了Priority接口的Runnable进行适配
// Use the adapter to adapt Runnable that implements the Priority interface
if (task instanceof Priority) {
return super.submit(new PriorityCallableAdapter((Priority) task, task));
}
return super.submit(new PriorityCallableAdapter(new DefaultPriority(), task));
}
// Priority的默认实现
// default implements of Priority
private final static class DefaultPriority implements Priority {
private int priority;
public DefaultPriority() {
}
public DefaultPriority(int priority) {
this.priority = priority;
}
@Override
public int priority() {
return priority;
}
@Override
public void priority(int priority) {
this.priority = priority;
}
}
// Priority与Runnable的适配器
// Priority and Runnable adapter
private final static class PriorityRunnableAdapter implements PriorityRunnable, PriorityComparable