【java】队列、优先队列、双端队列Deque、链表LinkedList、阻塞队列(Blocking queue)


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();
      }
    }
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值