【java】队列、优先队列、双端队列Deque、链表LinkedList、阻塞队列(Blocking queue)
2015-08-25 15:02
441 查看
package test; public class HelloWorld { public static void main(String[] args) { java.util.Queue<String> queue = new java.util.LinkedList<String>(); queue.offer("Oklahoma"); queue.offer("Indiana"); queue.offer("Georgia"); queue.offer("Texas"); while (queue.size() > 0) System.out.println(queue.remove() + " "); } }
package test; import java.util.*; public class HelloWorld { public static void main(String[] args) { //无参数构造方法创建字符串优先队列,因此按照升序从队列中删除 PriorityQueue<String> queue1 = new PriorityQueue<String>(); queue1.offer("Oklahoma"); queue1.offer("Indiana"); queue1.offer("Georgia"); queue1.offer("Texas"); System.out.println("Priority queue using Comparable:"); while (queue1.size() > 0) { System.out.print(queue1.remove() + " "); } //创建一个带指定初始容量和比较器的优先队列。从Collection.reverseOrder()中获得的比较器 //创建优先队列,该方法以逆序对元素排序,因此,字符串以降序从队列中删除 PriorityQueue<String> queue2 = new PriorityQueue<String>( 4, Collections.reverseOrder()); queue2.offer("Oklahoma"); queue2.offer("Indiana"); queue2.offer("Georgia"); queue2.offer("Texas"); System.out.println("\nPriority queue using Comparator:"); while (queue2.size() > 0) { System.out.print(queue2.remove() + " "); } } }
阻塞队列:
package test; import java.util.concurrent.*; public class HelloWorld { // 对于多线程而言,单个线程使用Thread,多个线程使用线程池。 // 都有其一个基本的格式,只要完成Run()方法编写 private static ArrayBlockingQueue<Integer> buffer = new ArrayBlockingQueue<Integer>(2); public static void main(String[] args) { // Create a thread pool with two threads ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new ProducerTask()); executor.execute(new ConsumerTask()); executor.shutdown(); } // A task for adding an int to the buffer private static class ProducerTask implements Runnable { public void run() { try { int i = 1; while (true) { System.out.println("Producer writes " + i); buffer.put(i++); // Add any value to the buffer, say, 1 // Put the thread into sleep Thread.sleep((int)(Math.random() * 10000)); } } catch (InterruptedException ex) { ex.printStackTrace(); } } } // A task for reading and deleting an int from the buffer private static class ConsumerTask implements Runnable { public void run() { try { while (true) { System.out.println("\t\t\tConsumer reads " + buffer.take()); // Put the thread into sleep Thread.sleep((int)(Math.random() * 10000)); } } catch (InterruptedException ex) { ex.printStackTrace(); } } } }
相关文章推荐
- 如何设置UILable的各种属性
- Cannot convert value '0000-00-00 00:00:00' TIMESTAMP
- Android测试三----TestSuite源码分析。
- requireJS的使用_API(1)
- 深入理解PHP之require/include顺序
- ZOJ 1649 Rescue
- request中可以获取到的内容
- 如何更新Android SDK和Build Tool
- UIViewController生命周期需要注意的
- UIView(包括子类)的几个初始化时执行动作的时机
- POJ 1141 Brackets Sequence (DP)
- leetcode 之 Unique Paths
- 关于android uid 与进程的关系
- POJ 1788 Building a New Depot(水~)
- iOS 并发编程之 Operation Queues
- UITableView的registerClass forCellReuseIdentifier用法详解
- UI第二讲UITextField和UIButton
- 报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library
- 关于Qt5 使用QQuickWidget加载QML背景设置透明的问题!
- 动态修改ui2dsprite的图片