Java PriorityBlockingQueue的问题
项目中有一个生产者消费者的情形,队列用了Java 的PriorityBlockingQueue,queue中的对象代码如下:
class TestCaseEntry implements Comparable<TestCaseEntry>{
private Long tcId;
private Integer days;
public TestCaseEntry(Long tcId, Integer days) {
this.tcId = tcId;
this.days = days;
}
public int compareTo(TestCaseEntry t) {
// TODO Auto-generated method stub
return this.days-t.days;
}
public String toString()
{
return this.days.toString();
}
public Long getTcId() {
return tcId;
}
public void setTcId(Long tcId) {
this.tcId = tcId;
}
public Integer getDays() {
return days;
}
public void setDays(Integer days) {
this.days = days;
}
}
需求就是每次从queue中取出days最小的若干个TestCaseEntry的对象。今天发现一个问题,系统运行了几天之后,queue不再是取最小的那个对象了。当时queue中有3000多个对象,days有0,5,10等值,queue的第一个元素竟然是days为10的,看系统的log,通过peek方法拿到的第一个元素确实是10。写了几个测试程序都没复现这个问题,但是系统中确实好几个queue都有这个问题。各位大牛谁知道咋回事?