VOOZH about

URL: https://www.geeksforgeeks.org/java/blockingqueue-put-method-in-java-with-examples/

⇱ BlockingQueue put() method in Java with examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

BlockingQueue put() method in Java with examples

Last Updated : 13 Sep, 2021

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:  


Output: 
Items in Queue are [Karan, Suraj, Harsh, Rahul]

 

Program 2: 


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: 


Output: 
Exception: java.lang.NullPointerException

 

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#put(E)

Comment