SynchronousQueue method element() in Java
A SynchronousQueue method element() retrieves the head of this queue without removing. It differs from peek() method where this method throws an exception if this queue is empty. It returns a head element of this queue, if not empty.
Syntax
 1 public E element()
return : It returns a head element of this queue, if not empty.

Exception:

1. NoSuchElementException : if this queue is empty
SynchronousQueue method element()
 1 package com.java;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.SynchronousQueue;
 6 import java.util.concurrent.TimeUnit;
 7 
 8 public class Main {
 9     static SynchronousQueue<Integer> queue = new SynchronousQueue<>();
 10     static ExecutorService executor = Executors.newFixedThreadPool(2);
 11 
 12     public static void main(String[] args) throws InterruptedException {
 13 
 14         Runnable producer = () -> {
 15             try {
 16                 queue.put(1000);
 17                 Integer res = queue.element();
 18                 System.out.println("A List elements : " + res);
 19             } catch (InterruptedException ex) {
 20                 System.out.println("An exception in producer : " + ex);
 21             }
 22         };
 23 
 24         Runnable consumer = () -> {
 25             try {
 26                 Integer element = queue.take();
 27                 System.out.println("A resulting element in consumer : " + element);
 28             } catch (InterruptedException e) {
 29                 throw new RuntimeException(e);
 30             }
 31         };
 32 
 33         executor.execute(producer);
 34         executor.execute(consumer);
 35 
 36         executor.awaitTermination(500, TimeUnit.MILLISECONDS);
 37         executor.shutdown();
 38     }
 39 }
In the above example, a SynchronousQueue of type Integer created and a two threads are created consumer and producer. A consumer thread wait for the element until a new element pushed into this queue. A producer thread put new element into this queue. A element() method called that throws an exception as this queue is empty.
Output
 1 A resulting element in consumer : 1000
 2 Exception in thread "pool-1-thread-1" java.util.NoSuchElementException
 3 	at java.base/java.util.AbstractQueue.element(AbstractQueue.java:136)
 4 	at com.java.Main.lambda$main$0(Main.java:17)
 5 	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
 6 	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
 7 	at java.base/java.lang.Thread.run(Thread.java:1570)

SynchronousQueue method element() with String

SynchronousQueue method element() with String
 1 package com.java;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.concurrent.ExecutorService;
 6 import java.util.concurrent.Executors;
 7 import java.util.concurrent.SynchronousQueue;
 8 import java.util.concurrent.TimeUnit;
 9 
 10 public class StringExp {
 11 
 12     static SynchronousQueue<String> queue = new SynchronousQueue<>();
 13     static ExecutorService executor = Executors.newFixedThreadPool(2);
 14 
 15     public static void main(String[] args) throws InterruptedException {
 16 
 17         Runnable producer = () -> {
 18             try {
 19                 queue.put("IOGyan");
 20                 String res = queue.element();
 21                 System.out.println("A List elements : " + res);
 22             } catch (InterruptedException ex) {
 23                 System.out.println("An exception in producer : " + ex);
 24             }
 25         };
 26 
 27         Runnable consumer = () -> {
 28             try {
 29                 String element = queue.take();
 30                 System.out.println("A resulting element in consumer : " + element);
 31             } catch (InterruptedException e) {
 32                 throw new RuntimeException(e);
 33             }
 34         };
 35 
 36         executor.execute(producer);
 37         executor.execute(consumer);
 38 
 39         executor.awaitTermination(500, TimeUnit.MILLISECONDS);
 40         executor.shutdown();
 41     }
 42 }
In the above example, a SynchronousQueue of type String created and a two threads are created consumer and producer. A consumer thread wait for the element until a new element pushed into this queue. A producer thread put new element into this queue. A element() method called that throws an exception as this queue is empty.
Output
 1 A resulting element in consumer : IOGyan
 2 Exception in thread "pool-1-thread-1" java.util.NoSuchElementException
 3 	at java.base/java.util.AbstractQueue.element(AbstractQueue.java:136)
 4 	at com.java.StringExp.lambda$main$0(StringExp.java:20)
 5 	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
 6 	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
 7 	at java.base/java.lang.Thread.run(Thread.java:1570)

SynchronousQueue method element() with custom object

SynchronousQueue method element() with custom object
 1 package com.java;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.SynchronousQueue;
 6 import java.util.concurrent.TimeUnit;
 7 
 8 public class Example {
 9 
 10     static SynchronousQueue<Product> queue = new SynchronousQueue<>();
 11     static ExecutorService executor = Executors.newFixedThreadPool(2);
 12 
 13     public static void main(String[] args) throws InterruptedException {
 14 
 15         Runnable producer = () -> {
 16             try {
 17                 Product pd1 = new Product(100, "Phone", 10, 120000.32);
 18                 queue.put(pd1);
 19                 Product res = queue.element();
 20                 System.out.println("A List elements : " + res);
 21             } catch (InterruptedException ex) {
 22                 System.out.println("An exception in producer : " + ex);
 23             }
 24         };
 25 
 26         Runnable consumer = () -> {
 27             try {
 28                 Product element = queue.take();
 29                 System.out.println("A resulting element in consumer : " + element);
 30             } catch (InterruptedException e) {
 31                 throw new RuntimeException(e);
 32             }
 33         };
 34 
 35         executor.execute(producer);
 36         executor.execute(consumer);
 37 
 38         executor.awaitTermination(500, TimeUnit.MILLISECONDS);
 39         executor.shutdown();
 40     }
 41 }
In the above example, a SynchronousQueue of type Product created and a two threads are created consumer and producer. A consumer thread wait for the element until a new element pushed into this queue. A producer thread put new element into this queue. A element() method called that throws an exception as this queue is empty.
Output
 1 Exception in thread "pool-1-thread-1" java.util.NoSuchElementException
 2 	at java.base/java.util.AbstractQueue.element(AbstractQueue.java:136)
 3 	at com.java.Example.lambda$main$0(Example.java:19)
 4 	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
 5 	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
 6 	at java.base/java.lang.Thread.run(Thread.java:1570)
 7 A resulting element in consumer : Phone 10 120000.32

Product class

Product class
 1 package com.java;
 2 
 3 public class Product implements Comparable<Product> {
 4 
 5     private final int pid;
 6     private final String name;
 7     private final long quantity;
 8     private final double price;
 9 
 10     public Product(int pid, String name, long quantity, double price) {
 11         this.pid = pid;
 12         this.name = name;
 13         this.quantity = quantity;
 14         this.price = price;
 15     }
 16 
 17     public int getPid() {
 18         return pid;
 19     }
 20 
 21     public String getName() {
 22         return name;
 23     }
 24 
 25     public long getQuantity() {
 26         return quantity;
 27     }
 28 
 29     public double getPrice() {
 30         return price;
 31     }
 32 
 33     @Override
 34     public String toString() {
 35         return this.name + " " + this.quantity + " " + this.price;
 36     }
 37 
 38     public boolean equals(Product p) {
 39         return this.getPid() == p.getPid();
 40     }
 41 
 42     @Override
 43     public int compareTo(Product p) {
 44         return this.getPid() - p.getPid();
 45     }
 46 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us