利用PriorityBlockingQueue实现线程池中带优先级的线程

转自https://www.chenx.tech/priority-blocking-queue/

简单来说,方案一

public class Task implements Runnable, Comparable<Task>

ThreadPoolExecutor executor = new ThreadPoolExecutor(..., new PriorityBlockingQueue<>(), ...);
executor.execute(task);

方案二
方案一中的Task 加上:

public class ComparableFutureTask extends FutureTask<Object> implements Comparable<ComparableFutureTask>

ThreadPoolExecutor executor = new ThreadPoolExecutor(..., new PriorityBlockingQueue<>(), ...);
executor.execute(new ComparableFutureTask(task));
    原文作者:qq_23204557
    原文地址: https://blog.csdn.net/qq_23204557/article/details/120258885
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞