无界队列
public class Task implements Runnable, Comparable<Task>{
private int priority;
public Task(int priority) {
this.priority = priority;
}
@Override
public int compareTo(Task o) {
return Integer.compare(this.priority, o.priority);
}
@Override
public void run() {
System.out.println("任务被处理了");
}
public int getPriority() {
return priority;
}
}
LinkedBlockingDeque
package com.qf.queue;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingDeque;
public class Test01 {
public static void main(String[] args) {
LinkedBlockingDeque<String> queue = new LinkedBlockingDeque<>();
queue.push("aaa");
queue.push("bbb");
queue.push("ccc");
queue.push("ddd");
queue.remove("ccc");
Iterator<String> it = queue.iterator();
while (it.hasNext()) {
String next = it.next();
System.out.println(next);
}
}
}
ArrayBlockingQueue
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
public class Test02 {
public static void main(String[] args) throws InterruptedException{
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(6);
queue.put("aaa");
queue.put("bbb");
queue.put("ccc");
queue.put("ddd");
queue.remove("ccc");
Iterator<String> iterator = queue.iterator();
while (iterator.hasNext()) {
String next = iterator.next();
System.out.println(next);
}
}
}
PriorityQueue
import java.util.PriorityQueue;
public class Test03 {
public static void main(String[] args) {
PriorityQueue<Task> queue = new PriorityQueue<>();
queue.add(new Task(1));
queue.add(new Task(2));
queue.add(new Task(3));
queue.add(new Task(4));
while (!queue.isEmpty()) {
Task poll = queue.poll();
System.out.println(poll.getPriority());
}
}
}