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
}
更多专业前端知识,请上 【猿2048】www.mk2048.com