DelayQueue 意外行为。 DrainTo 仅从队列中删除 1 个过期项

DelayQueue unexpected behavior. DrainTo deletes from the queue only 1 expired item

我想遍历我的 DelayQueue 中未过期的元素。 class交易实现了Delayed,有一个字段timestamp,表示交易发起时的UTC时间戳(不是当前时间戳)

public class Transaction implements Delayed {
    private final Double amount;
    private final Long timestamp;   //timestamp of a time when the item was created and send here


    public Transaction(double amount, long timestamp) {
        this.amount = amount;
        this.timestamp = timestamp;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long delay = unit.convert(ONEMINUTE - (System.currentTimeMillis() - timestamp), TimeUnit.MILLISECONDS);
    return delay;
}

    @Override
    public int compareTo(Delayed delayed) {
        if (delayed == this) {
            return 0;
        }

        if (delayed instanceof Transaction) {
            return 0;
        }

        long diff = (getDelay(TimeUnit.MILLISECONDS) - delayed.getDelay(TimeUnit.MILLISECONDS));
        return ((diff == 0) ? 0 : ((diff < 0) ? -1 : 1));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + timestamp.hashCode();
        return result;
    }

    @Override
    public boolean equals( Object obj ) {
        if( this == obj ) {
            return true;
        }

        if( obj == null ) {
            return false;
        }

        if( !( obj instanceof Transaction ) ) {
            return false;
        }

        final Transaction other = ( Transaction )obj;
        return timestamp.equals(other.timestamp);
    }
}

下面的 TransactionManager class 将传入的事务添加到队列中,如果新的传入事务小于 1 分钟。 在 getStatistics 上,旧的交易应该从队列中移除,并且队列应该只包含小于 1 分钟的交易

public class TransactionManager {

    private DelayQueue<Transaction> transactions;


    public TransactionManager() {
        transactions = new DelayQueue<>();
        System.setProperty("user.timezone", "UTC");
    }

    public Object createTransaction(String json) {
        JSONObject jsonObject = null;
        try {
            jsonObject = JsonValidator.validateTransactionJson(json);
        } catch (Exception ex) {
            return new ResponseEntity(HttpStatus.UNPROCESSABLE_ENTITY);
        }
        long delay = System.currentTimeMillis() - ((Long) jsonObject.get(TIMESTAMP));
        if (delay > ONEMINUTE) {
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        }
        transactions.add(new Transaction((Double) jsonObject.get(AMOUNT), (Long) jsonObject.get(TIMESTAMP)));
        return new ResponseEntity(HttpStatus.OK);
    }

    public long getStatistics() {
        List<Transaction> tempForCleaning = new ArrayList<>();
        transactions.drainTo(tempForCleaning);
        tempForCleaning.clear();
        StatisticJSON statistics = new StatisticJSON();
        transactions.stream().forEach(transaction -> {
            statistics.setCount(statistics.getCount() + 1);
        });
        return statistics.getCount();
    }
}

在这个测试中,我创建了 40 秒之前的 5 个事务和 10 秒之前的 3 个事务。 所以在等待 45 秒后,前 5 个应该被排出,队列应该只包含 3 个事务,然而,方法 drainTo 只删除 1 个旧事务。

@Test
public void test() {
    DateTime dateTime = new DateTime(DateTimeZone.UTC);
    long fortyMilliSecondsAgo = dateTime.minusSeconds(40).getMillis();
    long twentyMilliSecondsAgo = dateTime.minusSeconds(10).getMillis();
    for (int i = 0; i < 5; i++) {
        createTransaction(fortyMilliSecondsAgo);
    }
    for (int i = 0; i < 3; i++) {
        createTransaction(twentyMilliSecondsAgo);
    }
    Assert.assertTrue(transactionManager.getStatistics() == 8);
    try {
        TimeUnit.SECONDS.sleep(45);
        System.out.println("\n\n\n");
        Assert.assertTrue(transactionManager.getStatistics() == 3);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

private void createTransaction(long timestamp) {
    transactionManager.createTransaction("{\"amount\":100.0,\"timestamp\":" + timestamp + "}");
}

我遗漏了一些东西,为什么 drainTo 只删除了一个过期的项目,即使还剩下 4 个,但无法捕捉到哪里...

if (delayed instanceof Transaction) { return 0; }

看起来不对 - 如果您希望 compareTogetDelay() 保持一致,您可能应该删除该位。所以你的方法应该看起来像这样(使用静态导入):

public int compareTo(Delayed delayed) {
  return delayed == this
    ? 0
    : Long.compare(getDelay(MILLISECONDS), delayed.getDelay(MILLISECONDS));
}