The put(E e) method of BlockingQueue interface inserts element passed as parameter to method at the tail of this BlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to BlockingQueue.
Syntax:
public void put(E e) throws InterruptedException
Parameter: This method takes a mandatory parameter e which is the element to be inserted in LinkedBlockingQueue.
Return Value: The method does not return anything.
Exception: This method throws following exceptions:
- InterruptedException– when interruption occurred at time of waiting for queue to become available
- NullPointerException– if the element passed to method is null
Note: The put() method of BlockingQueue has been inherited from the Queue class in Java.
Below programs illustrates put(E e) method of BlockingQueue class:
Program 1:
Java
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
int capacityOfQueue = 4 ;
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
BQ.put( "Karan" );
BQ.put( "Suraj" );
BQ.put( "Harsh" );
BQ.put( "Rahul" );
System.out.println( "Items in Queue are " + BQ);
}
}
|
Output:
Items in Queue are [Karan, Suraj, Harsh, Rahul]
Program 2:
Java
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public void PutDemo() throws InterruptedException
{
int capacityOfQueue = 5 ;
BlockingQueue<Employee> BQ
= new LinkedBlockingQueue<Employee>(capacityOfQueue);
Employee emp1 = new Employee( "Ranjeet" , "Tester" , "29000" , 27 );
Employee emp2 = new Employee( "Sanjeet" , "Manager" , "98000" , 34 );
Employee emp3 = new Employee( "Karan" , "Analyst" , "44000" , 30 );
BQ.put(emp1);
BQ.put(emp2);
BQ.put(emp3);
System.out.println( "Details of Employees:" );
Iterator itr = BQ.iterator();
while (itr.hasNext())
System.out.println(itr.next());
}
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) {
e.printStackTrace();
}
}
}
|
Output:
Details of Employees:
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]
Program 3:
Java
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
int capacityOfQueue = 4 ;
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
try {
BQ.put( null );
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#put(E)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Sep, 2021
Like Article
Save Article