Thinking in java -- 队列

来源:互联网 发布:linux批量解压zip 编辑:程序博客网 时间:2024/06/18 01:31

运行下面代码

package containers;//: containers/QueueBehavior.java// Compares the behavior of some of the queuesimport java.util.concurrent.*;import java.util.*;import net.mindview.util.*;public class QueueBehavior {    private static int count = 10;    static <T> void test(Queue<T> queue, Generator<T> gen) {        for (int i = 0; i < count; i++)            queue.offer(gen.next());        while (queue.peek() != null)            System.out.print(queue.remove() + " ");        System.out.println();    }    static class Gen implements Generator<String> {        String[] s = ("one two three four five six seven " + "eight nine ten")                .split(" ");        int i;        public String next() {            return s[i++];        }    }    public static void main(String[] args) {        test(new LinkedList<String>(), new Gen());        test(new PriorityQueue<String>(), new Gen());        test(new ArrayBlockingQueue<String>(count), new Gen());        test(new ConcurrentLinkedQueue<String>(), new Gen());        test(new LinkedBlockingQueue<String>(), new Gen());        test(new PriorityBlockingQueue<String>(), new Gen());    }} /*     * Output: one two three four five six seven eight nine ten eight five four     * nine one seven six ten three two one two three four five six seven eight     * nine ten one two three four five six seven eight nine ten one two three     * four five six seven eight nine ten eight five four nine one seven six ten     * three two     */// :~

程序test方法,通过queue.offer生成队列,并再通过queue.remove()异常并输出头元素。
输出结果,

one two three four five six seven eight nine ten
eight five four nine one seven six ten three two
one two three four five six seven eight nine ten
one two three four five six seven eight nine ten
one two three four five six seven eight nine ten
eight five four nine one seven six ten three two

LinkedList:

List 接口的链接列表实现。实现所有可选的列表操作,并且允许所有元素(包括 null)。除了实现 List 接口外,LinkedList 类还为在列表的开头及结尾 get、remove 和 insert 元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列或双端队列。

PriorityQueue:

一个基于优先级堆的无界优先级队列。优先级队列的元素按照其自然顺序进行排序,或者根据构造队列时提供的 Comparator 进行排序,具体取决于所使用的构造方法。优先级队列不允许使用 null 元素。依靠自然顺序的优先级队列还不允许插入不可比较的对象(这样做可能导致 ClassCastException)。

ArrayBlockingQueue

一个由数组支持的有界阻塞队列。此队列按 FIFO(先进先出)原则对元素进行排序。队列的头部 是在队列中存在时间最长的元素。队列的尾部 是在队列中存在时间最短的元素。新元素插入到队列的尾部,队列获取操作则是从队列头部开始获得元素。

ConcurrentLinkedQueue

一个基于链接节点的无界线程安全队列。此队列按照 FIFO(先进先出)原则对元素进行排序。队列的头部 是队列中时间最长的元素。队列的尾部 是队列中时间最短的元素。新的元素插入到队列的尾部,队列获取操作从队列头部获得元素。当多个线程共享访问一个公共 collection 时,ConcurrentLinkedQueue 是一个恰当的选择。此队列不允许使用 null 元素。

LinkedBlockingQueue

一个基于已链接节点的、范围任意的 blocking queue。此队列按 FIFO(先进先出)排序元素。队列的头部 是在队列中时间最长的元素。队列的尾部 是在队列中时间最短的元素。新元素插入到队列的尾部,并且队列获取操作会获得位于队列头部的元素。链接队列的吞吐量通常要高于基于数组的队列,但是在大多数并发应用程序中,其可预知的性能要低。

PriorityBlockingQueue

一个无界阻塞队列,它使用与类 PriorityQueue 相同的顺序规则,并且提供了阻塞获取操作。虽然此队列逻辑上是无界的,但是资源被耗尽时试图执行 add 操作也将失败(导致 OutOfMemoryError)。此类不允许使用 null 元素。依赖自然顺序的优先级队列也不允许插入不可比较的对象(这样做会导致抛出 ClassCastException)。

更多内容,详见JDK1.6中文版。http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩子在家偷钱怎么办 小朋友被鸡抓伤怎么办 宝宝在学校不说怎么办? 宝宝不和小朋友玩怎么办 初中孩子不想上学怎么办 孩子装病不想上学怎么办 中学生叛逆不愿意上学怎么办 孩子去幼儿园哭闹怎么办 孩子哭闹不上学怎么办 孩子中班还哭怎么办 小孩子不爱上幼儿园怎么办 小孩子不爱去幼儿园怎么办 宝宝去幼儿园哭闹怎么办 宝宝上幼儿园哭闹怎么办 小朋友上幼儿园哭闹怎么办 迷路了怎么办幼儿故事 大班迷路了怎么办故事 玩手机眼睛干涩怎么办 宝睡觉不踏实怎么办 觉得自己老了怎么办 微信没自动扣费怎么办 东西放在家找不到怎么办 刚怀孕同房流产怎么办 开车遇上送葬的怎么办 流水钓鱼走漂怎么办 水库里小鱼太多怎么办 英国留学生怎么办澳签 老赖欠货款不还怎么办 老赖失信出国怎么办 打完预防针呕吐怎么办 媳妇老说离婚怎么办 老公吵架要出走怎么办 惹老婆生气了怎么办 第二次打了老婆怎么办 吵架打了老婆怎么办 我离婚我怎么办呢 把人打成重伤怎么办 车在水里憋死了怎么办 佛说犯了天规怎么办 一天想解大便怎么办 两个月小孩腹泻怎么办