Java ArrayBlockingQueue put() MethodThe put() method of ArrayBlockingQueue adds the defined element at the tail of this queue. It waits till the space becomes available if queue is full. Syntax:Parameters:e - This is the element to be added. Specified By:The put() method of ArrayBlockingQueue class is specified by put() method in interface BlockingQueue Throws:The put() method throws:
Example 1Output: Elements in queue : [67, 109, 76, 876, 2] Example 2Output: Error:(9, 18) java: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown Error:(10, 18) java: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown Error:(11, 18) java: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown Error:(12, 18) java: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown Either we should add exception to method signature or we should surround the put() method with try catch, else it will give above described error. Example 3Output: Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ArrayBlockingQueue.checkNotNull(ArrayBlockingQueue.java:150) at java.util.concurrent.ArrayBlockingQueue.put(ArrayBlockingQueue.java:348) at com.javaTpoint.ArrayBlockingQueuePutExample2.main(ArrayBlockingQueuePutExample2.java:13) If the specified element is null, it will give null pointer exception as shown above. Next TopicJava-arrayblockingqueue-removeall-method |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India