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

用法:

public E take() throws InterruptedException

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

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

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

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

示例1:使用take()删除LinkedBlockingQueue头上的操作。

// Java Program Demonstrate take()

// method of LinkedBlockingQueue

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {

public static void main(String[] args)

throws InterruptedException

{

// define capacity of LinkedBlockingQueue

int capacityOfQueue = 4;

// create object of LinkedBlockingQueue

LinkedBlockingQueue linkedQueue

= new LinkedBlockingQueue(capacityOfQueue);

// Add element to LinkedBlockingQueue

linkedQueue.add("Ravi");

linkedQueue.add("Suraj");

linkedQueue.add("Harsh");

linkedQueue.add("Sayan");

// print elements of queue

System.out.println("Items in Queue are " + linkedQueue);

// remove two elements from queue from head

// Applying take() method on queue to remove element

String removedItem1 = linkedQueue.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 "

+ linkedQueue);

// Applying take() method on queue to remove another element

String removedItem2 = linkedQueue.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 "

+ linkedQueue);

}

}

输出:

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:使用take()从LinkedBlockingQueue中删除Employee对象。

// Java Program Demonstrate take()

// method of LinkedBlockingQueue

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {

public void takeDemo() throws InterruptedException

{

// define capacity of LinkedBlockingQueue

int capacityOfQueue = 5;

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

// remove elements from the queue

// and follow this process again and again

// until the queue becomes empty

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

// Remove Employee item from LinkedBlockingQueue

// using take()

Employee removedEmp = linkedQueue.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 linkedQueue

int size = linkedQueue.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

java linkedblockingqueue_Java LinkedBlockingQueue take()用法及代码示例相关推荐

  1. java中skip的用法,Java PushbackReader skip(long)用法及代码示例

    Java中的PushbackReader类的skip(long)方法用于跳过流中指定数量的字符.此字符数被指定为参数.如果通过跳过到达流的末尾,它将阻塞流,直到它获得一些字符或抛出IOExceptio ...

  2. java sliplist_Java List retainAll()用法及代码示例

    此方法用于将指定集合中存在于集合中的所有元素保留到列表中. 用法: boolean retainAll(Collection c) 参数:此方法只有一个参数,即要在给定列表中保留哪些元素的集合. 返回 ...

  3. java doublebuffer_Java DoubleBuffer clear()用法及代码示例

    java.nio.CharBuffer类的clear()方法用于清除此缓冲区.在清除此缓冲区时,需要进行以下更改: 位置设置为零 限制设置为容量 商标被丢弃. 用法: public final Dou ...

  4. java filesystem_Java FileSystem isReadOnly()用法及代码示例

    FileSystem类的isReadOnly()方法用于检查此文件系统是否仅允许对其文件存储区进行只读访问.如果文件系统仅允许对其文件存储进行读取访问,则此方法将返回true,否则返回false. 用 ...

  5. java bidi_Java Bidi createLineBidi()用法及代码示例

    java.text.Bidi类的createLineBidi()方法用于创建具有相同基本方向并表示该范围内当前bidi的每个属性的新的bidi对象. 用法: public Bidi createLin ...

  6. java intfilter_Java IntStream filter()用法及代码示例

    IntStream filter(IntPredicate predicate)返回一个由与给定谓词匹配的流元素组成的流.这是一个中间操作.这些操作总是很懒惰,即执行诸如filter()之类的中间操作 ...

  7. java seconds_Java LocalTime minusSeconds()用法及代码示例

    LocalTime类的minusSeconds()方法用于从此LocalTime中减去指定的秒数,然后将结果作为LocalTime对象返回.这一瞬间是一成不变的.计算大约在午夜结束. 用法: publ ...

  8. java offsetdatetime_Java OffsetDateTime withHour()用法及代码示例

    Java中OffsetDateTime类的withHour()方法返回此OffsetDateTime的副本,其中一天中的小时数按照参数中的指定进行了更改. 用法: public OffsetDateT ...

  9. java isequal_Java LocalDate isEqual()用法及代码示例

    Java中的LocalDate类的isEqual()方法检查此日期是否等于指定的日期. 用法: public boolean isEqual(ChronoLocalDate date2) 参数:此方法 ...

最新文章

  1. Educational Codeforces Round 93 (Rated for Div. 2) 题解
  2. F5 Priority Group Activation
  3. python语言入门n-python面试题基础
  4. winscp 服务器拒绝了SFTP连接,但它监听FTP连接。 想要用FTP协议来代替SFTP吗?最好是用加密的。
  5. 项目管理中的组织计划
  6. Java SE有几个代码_JavaSE常用类及方法的介绍(附代码)
  7. 不能安装_钢厂为什么不能随意更换一氧化碳报警器安装位置
  8. python os操作
  9. 每日一笑 | IE的反射弧也太长了吧......
  10. 联想r630服务器开启虚拟化,整合虚拟化 联想万全R630服务器上市
  11. linux上安装osg_ubuntu 环境 安装OSG
  12. python中类型转换的规则_Python 类型转换指南
  13. OC 获取view相对位置_【黑苹果系列】小白教程之DSD补丁篇 | 7分钟教你优雅定制最关键的OC补丁(clover通用)...
  14. 产品经理必修30本书
  15. java对外接口开发实例
  16. visio-软件程序流程图规范
  17. 为什么增益裕度可以是负数
  18. matlab上位机电机,基于MATLAB的电机综合性能测试系统上位机软件设计
  19. msf介绍及其常用模块
  20. 如何给网站添加CNZZ站长统计功能代码的常用办法

热门文章

  1. 算法LeetCode解题(C++)-15. 四数之和(难度:中等)
  2. ssm毕设项目住院病人管理系统pebfh(java+VUE+Mybatis+Maven+Mysql+sprnig)
  3. 了解一些常用的文件系统和一些基础定义
  4. FFmpeg简单使用:音频编码 ---- pcm转aac
  5. 《医疗器械软件注册指导原则》阅读笔记
  6. 大连北站到大连计算机学校体育馆,大连北站到圣亚海洋世界怎么走
  7. Linux的pcie模拟网卡,Intel英特尔PCIe万兆网卡虚拟功能驱动4.11.1版For Linux(2021年3月5日发布)...
  8. 为老年人熟悉智能手机的APP
  9. matlab心碎的心,让人撕心裂肺的心痛网名,心碎绝望的伤感昵称
  10. 详解intel处理器命名规则