java队列,很实用,很重要 JavaBlog
程序员文章站
2022-03-29 12:11:49
...
http://www.iteye.com/topic/519260
有优先级的
http://jiangzhengjun.iteye.com/blog/565275
上面说的比较散乱,用ConcurrentLinkedQueue来做比较好,线程安全的。
package test;
import java.util.PriorityQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class QueueTest2
{
public static void main(String[] args)
{
// PriorityQueue priorityQueue = new PriorityQueue();
// priorityQueue.offer("Texas");
// priorityQueue.offer("Alabama");
// priorityQueue.offer("California");
// priorityQueue.offer("Rhode Island");
// int queueSize = priorityQueue.size();
// for (int i = 0; i < queueSize; i++)
// {
// System.out.println(priorityQueue.poll());
// }
ConcurrentLinkedQueue <String> highPriority = new ConcurrentLinkedQueue <String>(); //高优先级
highPriority.add("ddd2");
highPriority.add("ddd1");
highPriority.add("ddd3");
highPriority.add("ddd4");
System.out.println(highPriority.remove());
System.out.println(highPriority.remove());
System.out.println(highPriority.remove());
System.out.println(highPriority.remove());
}
}
有优先级的
http://jiangzhengjun.iteye.com/blog/565275
上面说的比较散乱,用ConcurrentLinkedQueue来做比较好,线程安全的。
package test;
import java.util.PriorityQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class QueueTest2
{
public static void main(String[] args)
{
// PriorityQueue priorityQueue = new PriorityQueue();
// priorityQueue.offer("Texas");
// priorityQueue.offer("Alabama");
// priorityQueue.offer("California");
// priorityQueue.offer("Rhode Island");
// int queueSize = priorityQueue.size();
// for (int i = 0; i < queueSize; i++)
// {
// System.out.println(priorityQueue.poll());
// }
ConcurrentLinkedQueue <String> highPriority = new ConcurrentLinkedQueue <String>(); //高优先级
highPriority.add("ddd2");
highPriority.add("ddd1");
highPriority.add("ddd3");
highPriority.add("ddd4");
System.out.println(highPriority.remove());
System.out.println(highPriority.remove());
System.out.println(highPriority.remove());
System.out.println(highPriority.remove());
}
}