java blockingqueue_Java BlockingQueue take()用法及代码示例

BlockingQueue接口的take()方法用于检索和删除此队列的头。如果队列为空,则它将等待直到元素可用。如果在线程上工作并在该过程中使用BlockingQueue,则此方法效率更高。因此,如果没有可用的元素,则最初调用take()的线程将进入睡眠状态,从而让其他线程执行所需的任何操作。

用法:

public E take() throws InterruptedException

返回值:此方法在此BlockingQueue的开头返回值。如果队列为空,则它将等待直到元素可用。

异常:此方法引发以下异常:

InterruptedException–如果队列为空,则在等待元素变为可用时发生中断。

注意:BlockingQueue的take()方法已从Java中的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 BQ

= new LinkedBlockingQueue(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 Program Demonstrate take()

// method of BlockingQueue

import java.util.concurrent.LinkedBlockingQueue;

import java.util.concurrent.BlockingQueue;

public class GFG {

public void takeDemo() throws InterruptedException

{

// define capacity of BlockingQueue

int capacityOfQueue = 5;

// create object of BlockingQueue

BlockingQueue BQ

= new LinkedBlockingQueue(capacityOfQueue);

// Add element to BlockingQueue

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

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

// Add Employee Objects to BQ

BQ.add(emp1);

BQ.add(emp2);

// remove elements from the queue

// and follow this process again and again

// until the queue becomes empty

while (BQ.size() != 0) {

// Remove Employee item from BlockingQueue

// using take()

Employee removedEmp = BQ.take();

// print removedItem

System.out.println("Removed Item is :");

System.out.println("Employee Name - "

+ removedEmp.name);

System.out.println("Employee Position - "

+ removedEmp.position);

System.out.println("Employee Salary - "

+ removedEmp.salary);

// find size of BQ

int size = BQ.size();

// print remaining capacity value

System.out.println("\nSize of list :" + size + "\n");

}

}

// 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();

try {

gfg.takeDemo();

}

catch (Exception e) {

e.printStackTrace();

}

}

}

输出:

Removed Item is :

Employee Name - Ravi

Employee Position - Tester

Employee Salary - 39000

Size of list :1

Removed Item is :

Employee Name - Sanjeet

Employee Position - Manager

Employee Salary - 98000

Size of list :0

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值