java delayqueue_Java DelayQueue size()用法及代码示例

Java中的DelayQueue类的size()方法用于返回给定队列中存在的元素数。如果元素数大于Integer.MAX_VALUE,则返回Integer.MAX_VALUE作为此DelayQueue的大小。

句法:

public int size()

参数:它不接受任何参数。

返回值:它返回一个整数,它是队列的长度。

以下示例程序旨在说明Java中DelayQueue的size()方法:

// Java program to illutrate the size() method

// of DelayQueue class

import java.util.concurrent.DelayQueue;

import java.util.concurrent.Delayed;

import java.util.concurrent.TimeUnit;

public class GFG {

public static void main(String args[])

{

// Create a DelayQueue Instance

DelayQueue queue = new DelayQueue();

// Create an object of type Delayed

Delayed obj = new Delayed() {

public long getDelay(TimeUnit unit)

{

return 24; // some value is returned

}

public int compareTo(Delayed o)

{

if (o.getDelay(TimeUnit.DAYS) >

this.getDelay(TimeUnit.DAYS))

return 1;

else if (o.getDelay(TimeUnit.DAYS) ==

this.getDelay(TimeUnit.DAYS))

return 0;

return -1;

}

};

// Insert the object to this DelayQueue

// Size of the queue after insertion will be 1

queue.add(obj);

// print the size of this DelayQueue using

// size() method

System.out.println("Size : " + queue.size());

}

}

输出:

Size : 1

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值