Java中的BlockingQueue put()方法及其示例
Java中的 BlockingQueue 接口的 put(E e) 方法将作为参数传递的元素插入到此BlockingQueue的尾部,如果队列未满。如果队列已满,则此方法将等待空间变得可用,并在空间可用后将元素插入到BlockingQueue中。
语法:
public void put(E e) throws InterruptedException
参数: 此方法接受一个必填参数 e ,它是要插入到LinkedBlockingQueue中的元素。
返回值: 此方法不返回任何内容。
异常: 此方法抛出以下异常:
- InterruptedException – 等待队列可用时发生中断时
- NullPointerException – 如果传递给该方法的元素为null
注意: Java中的 BlockingQueue 的 put() 方法已从Java中的 Queue 类继承。
下面的程序演示了BlockingQueue类的put(E e)方法:
程序1:
// Java程序演示BlockingQueue的put(E e)方法
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// 定义BlockingQueue的容量
int capacityOfQueue = 4;
// 创建BlockingQueue对象
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
// 使用put()方法添加元素
BQ.put("Karan");
BQ.put("Suraj");
BQ.put("Harsh");
BQ.put("Rahul");
// 打印队列中的元素
System.out.println("Items in Queue are " + BQ);
}
}
输出:
Items in Queue are [Karan, Suraj, Harsh, Rahul]
程序2:
// Java程序演示了BlockingQueue的put()方法
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public void PutDemo() throws InterruptedException
{
// 定义BlockingQueue的容量
int capacityOfQueue = 5;
// 创建BlockingQueue对象
BlockingQueue<Employee> BQ
= new LinkedBlockingQueue<Employee>(capacityOfQueue);
// 向BlockingQueue添加元素
Employee emp1 = new Employee("Ranjeet", "Tester", "29000", 27);
Employee emp2 = new Employee("Sanjeet", "Manager", "98000", 34);
Employee emp3 = new Employee("Karan", "Analyst", "44000", 30);
// 使用put(E e)将Employee对象添加到BlockingQueue中
BQ.put(emp1);
BQ.put(emp2);
BQ.put(emp3);
System.out.println("员工的详细信息:");
// 打印BlockingQueue的详细信息
Iterator itr = BQ.iterator();
while (itr.hasNext())
System.out.println(itr.next());
}
// 创建Employee对象,包括姓名、职位、年龄和工资等属性
public class Employee {
public String name;
public String position;
public String salary;
public int Age;
Employee(String name, String position,
String salary, int age)
{
this.name = name;
this.position = position;
this.salary = salary;
this.Age = age;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", position="
+ position + ", salary=" + salary
+ ", Age=" + Age + "]";
}
}
// 主方法
public static void main(String[] args)
{
GFG gfg = new GFG();
try {
gfg.PutDemo();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出:
员工的详细信息:
Employee [name=Ranjeet, position=Tester, salary=29000, Age=27]
Employee [name=Sanjeet, position=Manager, salary=98000, Age=34]
Employee [name=Karan, position=Analyst, salary=44000, Age=30]
程序3:
// Java程序演示了BlockingQueue的put(E e)方法
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// 定义BlockingQueue的容量
int capacityOfQueue = 4;
// 创建BlockingQueue对象
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
// 尝试将null值放入put()方法里
try {
BQ.put(null);
}
catch (Exception e) {
// 打印错误详情
System.out.println("异常:" + e);
}
}
}
输出:
异常:java.lang.NullPointerException
参考资料: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#put(E)
极客教程