我正在构建一个REST服务,我有以下类的结构:
public class MyTask implements Runnable {
@Override
public void run() {
//do stuff
}
}
public class RestController {
private MyThreadPool myThreadPool;
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public ResponseEntity<?> getSomething (....){
myThreadPool.getExecutorService().submit(new MyTask(request));
}
}
public class MyThreadPool{
private final static Integer SIZE_OF_POOL = 5;
private final static Integer KEEP_ALIVE= 5000;
private final static Integer QUEUE_SIZE = 50;
private final static Integer MAX_POOL_SIZE = 10;
private BlockingQueue<Runnable> queue = new PriorityBlockingQueue<Runnable>(QUEUE_SIZE); //pass comparator here?
private ExecutorService executorService = new ThreadPoolExecutor(SIZE_OF_POOL, MAX_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
public ExecutorService getExecutorService() {
return executorService;
}
}
我的项目中可能有我的request
对象有一个整数来表示优先级的场景。
我希望使用此优先级值提交可执行的任务以执行,我不确定在何处实现此逻辑以及如何执行此操作。我知道我必须在某处使用PriorityBlockingQueue
并实现Comparator
。我也是......
1)在MyThreadPool类中实现 2)或在MyTask中实现逻辑
请问您能说明如何实施此比较器,以便首先执行具有最高优先级的请求吗?