Java中的BlockingQueue take()方法(附例子)
BlockingQueue 接口中的 take() 方法用于检索和删除此队列的头部。如果队列为空,它将等待直到有元素可用。如果在线程中使用BlockingQueue,则此方法更有效率。因此,最初调用take()的线程会进入睡眠状态,直到有元素可用,其它线程则可以做它们需要做的事情。
语法:
public E take() throws InterruptedException
返回值: 如果队列为空,则此方法返回此BlockingQueue的头部值,并等待直到有元素可用。
异常: 此方法会抛出以下异常:
- InterruptedException – 当在等待一个元素变得可用时,如果队列为空,就会发生中断。
注意: : Java中的 BlockingQueue take() 方法已从 Queue 类继承了下来。
下面的程序演示了BlockingQueue接口的take()方法:
程序1:
代码如下:
// Java Program Demonstrate take()
// method of BlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of BlockingQueue
int capacityOfQueue = 4;
// create object of BlockingQueue
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to BlockingQueue
BQ.add("Ravi");
BQ.add("Suraj");
BQ.add("Harsh");
BQ.add("Sayan");
// print elements of queue
System.out.println("Items in Queue are " + BQ);
// remove two elements from queue from head
// Applying take() method on queue to remove element
String removedItem1 = BQ.take();
// print removedItem and queue
System.out.println("Removed Item from head is "
+ removedItem1);
// print elements of queue after removing first item
System.out.println("Remaining Items in Queue are "
+ BQ);
// Applying take() method on queue to remove another element
String removedItem2 = BQ.take();
// print removedItem and queue
System.out.println("Removed Item from head is "
+ removedItem2);
// print elements of queue after removing first item
System.out.println("Remaining Items in Queue are "
+ BQ);
}
}
输出:
Items in Queue are [Ravi, Suraj, Harsh, Sayan]
Removed Item from head is Ravi
Remaining Items in Queue are [Suraj, Harsh, Sayan]
Removed Item from head is Suraj
Remaining Items in Queue are [Harsh, Sayan]
程序2:
// Java程序演示BlockingQueue的take()方法
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public void takeDemo() throws InterruptedException
{
// 定义BlockingQueue的容量
int capacityOfQueue = 5;
// 创建BlockingQueue对象
BlockingQueue<Employee> BQ
= new LinkedBlockingQueue<Employee>(capacityOfQueue);
// 添加元素到BlockingQueue
Employee emp1 = new Employee("Ravi", "测试员", "39000");
Employee emp2 = new Employee("Sanjeet", "经理", "98000");
// 将员工对象添加到BQ
BQ.add(emp1);
BQ.add(emp2);
// 从队列中删除元素,
// 并一遍又一遍地按此过程继续,
// 直到队列变为空为止
while (BQ.size() != 0) {
// 使用take()方法从BlockingQueue中删除Employee项
Employee removedEmp = BQ.take();
// 打印已删除的项
System.out.println("已删除的项是:");
System.out.println("员工姓名 - "
+ removedEmp.name);
System.out.println("员工职位 - "
+ removedEmp.position);
System.out.println("员工工资 - "
+ removedEmp.salary);
// 查找BQ的大小
int size = BQ.size();
// 打印剩余容量值
System.out.println("\n列表大小:" + size + "\n");
}
}
// 使用名称、职位和工资作为属性创建一个Employee对象
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 + "]";
}
}
// 主方法
public static void main(String[] args)
{
GFG gfg = new GFG();
try {
gfg.takeDemo();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
已删除的项是:
员工姓名 - Ravi
员工职位 - 测试员
员工工资 - 39000
列表大小:1
已删除的项是:
员工姓名 - Sanjeet
员工职位 - 经理
员工工资 - 98000
列表大小:0
极客教程