java linkedblockingqueue capacity,Java LinkedBlockingQueue remainingCapacity()用法及代码示例

LinkedBlockingQueue的remainingCapacity()方法返回可以无限制添加到LinkedBlockingQueue的更多元素的数量。

返回的Thr容量出现三种情况:

如果剩余容量为零,则不能再将其他元素添加到LinkedBlockingQueue。

如果LinkedBlockingQueue的剩余Capacity等于Queue的大小,则无法从队列中删除任何元素,因为在这种情况下队列为空。

在任何其他情况下,Capacity始终等于此LinkedBlockinQueue的初始容量和此LinkedBlockingQueue当前大小之间的差。

用法:

public int remainingCapacity()

返回值:此方法返回LinkedBlockingQueue的剩余容量。

以下示例程序旨在说明LinkedBlockingQueue类的remainingCapacity()方法:

示例1:使用remainingCapacity()方法返回LinkedBlockingQueue的剩余容量,其中LinkedBlockingQueue包含名称列表。

// Java Program Demonstrate remainingCapacity()

// method of LinkedBlockingQueue

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {

public static void main(String[] args)

{

// define capacity of LinkedBlockingQueue

int capacityOfQueue = 7;

// create object of LinkedBlockingQueue

LinkedBlockingQueue linkedQueue

= new LinkedBlockingQueue(capacityOfQueue);

// Add element to LinkedBlockingQueue

linkedQueue.add("John");

linkedQueue.add("Tom");

linkedQueue.add("Clark");

linkedQueue.add("Kat");

// find remaining Capacity  of linkedQueue

// using remainingCapacity() method

int remainingCapacity = linkedQueue.remainingCapacity();

// print result

System.out.println("Queue is " + linkedQueue);

// print head of queue

System.out.println("Remaining Capacity of Queue is "

+ remainingCapacity);

}

}

输出:

Queue is [John, Tom, Clark, Kat]

Remaining Capacity of Queue is 3

示例2:查找包含员工列表的LinkedBlockingQueue的剩余容量。

// Java Program Demonstrate peek()

// method of LinkedBlockingQueue

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {

public void findPeek()

{

// define capacity of LinkedBlockingQueue

int capacityOfQueue = 7;

// create object of LinkedBlockingQueue

LinkedBlockingQueue linkedQueue

= new LinkedBlockingQueue(capacityOfQueue);

// Add element to LinkedBlockingQueue

Employee emp1 = new Employee("Ravi", "Tester", "39000");

Employee emp2 = new Employee("Sanjeet", "Manager", "98000");

// Add Employee Objects to linkedQueue

linkedQueue.add(emp1);

linkedQueue.add(emp2);

// add element and find remaining capacity

// follow same process again

// until the queue becomes full

while (linkedQueue.size() != 7) {

// adding emp2 again and again to queue

System.out.println("Adding employee is success "

+ linkedQueue.offer(emp2));

// find remaining capacity of linkedQueue

// using remainingCapacity() method

int remain = linkedQueue.remainingCapacity();

// print remaining capacity  value

System.out.println("Remaining Capacity of list :"

+ remain);

}

}

// create an Employee Object with name,

// position and salary as an attribute

public class Employee {

public String name;

public String position;

public String salary;

Employee(String name, String position, String salary)

{

this.name = name;

this.position = position;

this.salary = salary;

}

@Override

public String toString()

{

return "Employee [name=" + name + ", position="

+ position + ", salary=" + salary + "]";

}

}

// Main Method

public static void main(String[] args)

{

GFG gfg = new GFG();

gfg.findPeek();

}

}

输出:

Adding employee is success true

Remaining Capacity of list :4

Adding employee is success true

Remaining Capacity of list :3

Adding employee is success true

Remaining Capacity of list :2

Adding employee is success true

Remaining Capacity of list :1

Adding employee is success true

Remaining Capacity of list :0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值