java数据结构之(二):Queue简介

1.什么是队列

    队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

    在队列这种数据结构中,最先插入的元素将是最先被删除的元素;反之最后插入的元素将是最后被删除的元素,因此队列又称为“先进先出”(FIFO—first in first out)的线性表。


2.队列常用的方法

(1).add:增加一个元索,如果队列已满,则抛出一个IIIegaISlabEepeplian异常;

(2).remove:移除并返回队列头部的元素 ,如果队列为空,则抛出一个NoSuchElementException异常;

(3).element:返回队列头部的元素,如果队列为空,则抛出一个NoSuchElementException异常;

(4).offer:添加一个元素并返回true,如果队列已满,则返回false;

(5).poll:移除并返问队列头部的元素,如果队列为空,则返回null;

(6).peek:返回队列头部的元素, 如果队列为空,则返回null;        

(7).put:添加一个元素,如果队列满,则阻塞;

(8).take:移除并返回队列头部的元素,如果队列为空,则阻塞;


3.队列(Queue)的类关系图



4.PriorityQueue(优先队列)

    优先级队列是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。如果不提供Comparator的话,优先队列中元素默认按自然顺序排列,也就是数字默认是小的在队列头,字符串则按字典序排列。


5.BlockingQueue(阻塞队列)

    BlockingQueue,顾名思义,阻塞队列。BlockingQueue是在java.util.concurrent下的,因此不难理解,BlockingQueue是为了解决多线程中数据高效安全传输而提出的。

    多线程中,很多场景都可以使用队列实现,比如经典的生产者/消费者模型,通过队列可以便利地实现两者之间数据的共享,定义一个生产者线程,定义一个消费者线程,通过队列共享数据就可以了。

    当然现实不可能都是理想的,比如消费者消费速度比生产者生产的速度要快,那么消费者消费到 一定程度上的时候,必须要暂停等待一下了(使消费者线程处于WAITING状态)。BlockingQueue的提出,就是为了解决这个问题的,他不用程序员去控制这些细节,同时还要兼顾效率和线程安全。

    阻塞队列所谓的"阻塞",指的是某些情况下线程会挂起(即阻塞),一旦条件满足,被挂起的线程又会自动唤醒。使用BlockingQueue,不需要关心什么时候需要阻塞线程,什么时候需要唤醒线程,这些内容BlockingQueue都已经做好了。


6.常见的BlockingQueue介绍

  (1).ArrayBlockingQueue: 基于数组的阻塞队列,必须指定队列大小。 比较简单。ArrayBlockingQueue中只有一个ReentrantLock对象,这意味着生产者和消费者无法并行运行(见下面的代码)。另 外,创建ArrayBlockingQueue时,可以指定ReentrantLock是否为公平锁,默认采用非公平锁。

  (2).LinkedBlockingQueue: 基于链表的阻塞队列,和ArrayBlockingQueue差不多。不过LinkedBlockingQueue如果不指定队列容量大小,会默认 一个类似无限大小的容量,之所以说是类似是因为这个无限大小是Integer.MAX_VALUE,这么说就好理解ArrayBlockingQueue 为什么必须要制定大小了,如果ArrayBlockingQueue不指定大小的话就用Integer.MAX_VALUE,那将造成大量的空间浪费,但是基于链表实现就不一样的,一个一个节点连起来而已。另外,LinkedBlockingQueue生产者和消费者都有自己的锁(见下面的代码),这意味着生产者和消费者可以"同时"运行。

  (3).PriorityBlockingQueue: PriorityBlockingQueue是一个没有边界的队列,它的排序规则和 java.util.PriorityQueue一样。需要注意,PriorityBlockingQueue中允许插入null对象。

  (4).SynchronousQueue: 一种没有缓冲的等待队列。每一个插入操作必须等待一个线程对应的移除操作。SynchronousQueue又有两种模式:公平模式和非公平模式。

  (5).DelayQueue:延迟型阻塞队列

  DelayQueue是一个BlockingQueue,其特化的参数是Delayed。Delayed扩展了Comparable接口,比较的基准为延时的时间值,Delayed接口的实现类getDelay的返回值应为固定值(final)。DelayQueue内部是使用PriorityQueue实现的。
  简单来说:
DelayQueue = BlockingQueue + PriorityQueue + Delayed
DelayQueue是一个使用优先队列(PriorityQueue)实现的BlockingQueue,优先队列的比较基准值是时间。


7.DelayQueue适合使用的一些实际开发场景

  (1).关闭空闲连接。服务器中,有很多客户端的连接,空闲一段时间之后需要关闭之。
  (2).缓存。缓存中的对象,超过了空闲时间,需要从缓存中移出。
  (3).任务超时处理。在网络协议滑动窗口请求应答式交互时,处理超时未响应的请求。


8.PriorityQueue代码示例

package com.biyao.current.priorityqueue;
/**
 * @ClassName: Student
 * @Description: 学生类实体
 * @author yangy
 * @date 2017年11月14日 下午3:04:41
 */
public class Student {
	
	private int id;
	
	private String name;
	
	public Student(int id,String name){
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}
package com.biyao.current.priorityqueue;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;

/**
 * @ClassName: PriorityQueueExample
 * @Description: 优先队列测试类
 * @author yangy
 * @date 2017年11月14日 下午3:13:20
 */
public class PriorityQueueExample {
	
	public static void main(String[] args) {
		
		//优先队列自然排序示例
        Queue<Integer> integerPriorityQueue = new PriorityQueue<Integer>(7);
        Random rand = new Random();
        for(int i=0;i<7;i++){
            integerPriorityQueue.add(new Integer(rand.nextInt(100)));
        }
        for(int i=0;i<7;i++){
            Integer in = integerPriorityQueue.poll();
            System.out.println("Processing Integer:"+in);
        }
        
      //Comparator实现
       Comparator<Student> idComparator = new Comparator<Student>(){
	        @Override
	        public int compare(Student c1, Student c2) {
	            return (int) (c1.getId() - c2.getId());
	        }
        };
        
        Queue<Student> studentPriorityQueue = new PriorityQueue<Student>(7,idComparator);
        for(int i=0; i<7; i++){
            int id = rand.nextInt(100);
            studentPriorityQueue.add(new Student(id, "yangy "+id + "号"));
        }
        
        while(true){
        	Student student = studentPriorityQueue.poll();
            if(student == null){
            	break;
            }
            System.out.println("学生信息:【id="+student.getId()+",name="+student.getName()+"】");
        }
		
	}
	
}
运行效果图:



9.DelayQueue代码示例

package com.biyao.current.delayqueue2;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: Student
 * @Description: 学生实体类
 * @author yangy
 * @date 2017年11月17日 下午1:10:57
 */
public class Student implements Runnable,Delayed{
	
	  private String name;  //姓名
	  private long costTime;//做试题的时间
	  private long finishedTime;//完成时间

	  public Student(String name, long costTime) {
	         this. name = name;
	         this. costTime= costTime;
	         finishedTime = costTime + System. currentTimeMillis();
	  }

	  @Override
	  public void run() {
	        System. out.println( name + " 交卷,用时" + costTime /1000 + "s");
	  }

	  @Override
	  public long getDelay(TimeUnit unit) {
	         return ( finishedTime - System.currentTimeMillis());
	  }

	  @Override
	  public int compareTo(Delayed o) {
	        Student other = (Student) o;
	         return costTime >= other. costTime?1:-1;
	  }
}

package com.biyao.current.delayqueue2;

import java.util.Random;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName: Teacher
 * @Description: 老师实体类
 * @author yangy
 * @date 2017年11月17日 下午1:12:48
 */
public class Teacher {
	
	  static final int STUDENT_SIZE = 30;
	  public static void main(String[] args) throws InterruptedException {
	        Random r = new Random();
	        //把所有学生看做一个延迟队列
	        DelayQueue<Student> students = new DelayQueue<Student>();
	        
	        students.put( new Student( "学生-yangy", 10000));
	        Student student = students.take();
	        student.run();
	        
	        //构造一个线程池用来让学生们“做作业”
	        ExecutorService exec = Executors.newFixedThreadPool(STUDENT_SIZE);
	         for ( int i = 0; i < STUDENT_SIZE; i++) {
               //初始化学生的姓名和做题时间
               students.put( new Student( "学生" + (i + 1), 3000 + r.nextInt(10000)));
	        }
	        //开始做题
	        while(! students.isEmpty()){
	           exec.execute( students.take());
	        }
	        exec.shutdown();
	        
	  }
}
运行效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值