public class Main { /** * 程式入口 */ public void start() { // 第一種,先進先出的隊列 // YolandaLinkedQueue queue = new YolandaLinkedQueue(3); // queue.start(); //..... ...
public class Main { /** * 程式入口 */ public void start() { // 第一種,先進先出的隊列 // YolandaLinkedQueue queue = new YolandaLinkedQueue(3); // queue.start(); // 第二種,沒有順序的隊列 YolandaQueue queue = new YolandaQueue(1); queue.start(); // 往隊列中添加請求 for (int i = 0; i < 20; i++) { Request request = new Request("請求" + i); if (i == 10) request.setPriority(Priority.C); if (i == 15) request.setPriority(Priority.D); queue.add(request); } } public static void main(String[] args) { Main main = new Main(); main.start(); } } public class YolandaLinkedQueue { private BlockingQueue<Request> blockingQueue; private TaskExecutor[] taskExecutors; public YolandaLinkedQueue(int poolSize) { // LinkedBlockingQueue是一個先進先出的隊列 blockingQueue = new LinkedBlockingQueue<>(); taskExecutors = new TaskExecutor[poolSize]; } public void add(Request request) { blockingQueue.add(request); } public void start() { for (int i = 0; i < taskExecutors.length; i++) { taskExecutors[i] = new TaskExecutor(blockingQueue); taskExecutors[i].start(); } } public void stop() { for (TaskExecutor taskExecutor : taskExecutors) { taskExecutor.setRunning(false); taskExecutor.interrupt(); } } } public class YolandaQueue { private BlockingQueue<Request> blockingQueue; private TaskExecutor[] taskExecutors; private AtomicInteger atomicInteger = new AtomicInteger(); public YolandaQueue(int poolSize) { // 如果Comparable#compareTo(Object)方法不做比較返回0,那麼是無序的 blockingQueue = new PriorityBlockingQueue<Request>(); taskExecutors = new TaskExecutor[poolSize]; } public void add(Request request) { request.setOrder(atomicInteger.incrementAndGet()); blockingQueue.add(request); } public void start() { for (int i = 0; i < taskExecutors.length; i++) { taskExecutors[i] = new TaskExecutor(blockingQueue); taskExecutors[i].start(); } } public void stop() { for (TaskExecutor taskExecutor : taskExecutors) { taskExecutor.setRunning(false); taskExecutor.interrupt(); } } } public class TaskExecutor extends Thread { private BlockingQueue<Request> blockingQueue; private boolean isRunning = true; public TaskExecutor(BlockingQueue<Request> blockingQueue) { this.blockingQueue = blockingQueue; } /** * @param isRunning the isRunning to set */ public void setRunning(boolean isRunning) { this.isRunning = isRunning; } @Override public void run() { while (isRunning) { Request request = null; try { // take方法是一個阻塞的方法,每次調用會拿到隊列中的第一個任務,如果隊列為空,這個方法將一直阻塞,知道隊列中有任務再次返回 request = blockingQueue.take(); } catch (InterruptedException e) { return; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(request.getName()); } } } public class Request implements Comparable<Request> { private String name; private Priority mPriority = Priority.B; private int order; /** * @param name */ public Request(String name) { super(); this.name = name; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @param mPriority the mPriority to set */ public void setPriority(Priority mPriority) { this.mPriority = mPriority; } /** * @return the mPriority */ public Priority getPriority() { return mPriority; } /** * @return the order */ public int getOrder() { return order; } /** * @param order the order to set */ public void setOrder(int order) { this.order = order; } @Override public int compareTo(Request other) { // 返回正數代表1排在2後面,返回負數表示1排在2前面 Priority priority = getPriority();// 拿到自身的優先順序 Priority otherPriority = other.getPriority(); return priority == otherPriority ? getOrder() - other.getOrder() : otherPriority.ordinal() - priority.ordinal(); } } public enum Priority { /** * 優先順序最低 */ A, /** * 預設優先順序 */ B, /** * 優先順序最高 */ C, /** * 一般情況下不用;特殊情況下,請求假如到到隊列後立即執行 */ D }