阻塞隊列,測試了下優先級隊列,如下是測試代碼,在隊列中用以,優先充值或者發送短信的時候使用。 java
@Test public void testPriorityBlockingQueue() throws InterruptedException { StudentComparator studentComparator = new StudentComparator(); BlockingQueue<Student> blockingQueue = new PriorityBlockingQueue<Student> (10,studentComparator); Student student1 = new Student(); student1.setId(1); student1.setName("學生1"); Student student2 = new Student(); student2.setId(3); student2.setName("學生3"); Student student3 = new Student(); student3.setId(2); student3.setName("學生2"); blockingQueue.offer(student1); blockingQueue.offer(student2); blockingQueue.offer(student3); while(true){ Student student= blockingQueue.take(); System.out.println(student); } } class Student { private int id; private String 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; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; } } class StudentComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { if(o1.getId()<=o2.getId()){ return 1; } return -1; } }