size() method of BlockingDeque Interface in Java.

size() method of BlockingDeque Interface in Java.


Posted in : Core Java Posted on : March 4, 2011 at 6:32 PM Comments : [ 0 ]

In this section we will discuss how can size ( ) method be implemented in BlockingDeque interface in java. Using this method you can find the total number of elements of an existing deque. If deque contains more than Integer.MAX_VALUE elements, it returns the same.

size() method of BlockingDeque Interface in Java.

In this section we will discuss how can size ( ) method be implemented in BlockingDeque interface in java.

Syntax

int size ( )

This method gives the number of elements of an existing deque.

Using this method you can find the total number of elements of an existing deque. If deque contains more than Integer.MAX_VALUE elements, it returns the same.

Parameter Description 

This method has no parameter.

Example of size() method

In this example we will show you how does size() method work in BlockingDeque interface. This example will help you to understand how can you find the total number of elements of a deque. 

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class Size implements Runnable{
  String name;
  BlockingDeque<Integer> bdq;
  public Size(String name,BlockingDeque<Integer> bdq){
    this.name= name;
   this.bdq=bdq;
  }
  public void run(){
    int i;
    boolean bol= bdq.isEmpty();
    System.out.println("Deque is empty "+bol);
    if(bol==false){
    System.out.println("Elements of deque are :"+bdq);
    try{
      Thread.sleep(500);
      int s = bdq.size();      
      System.out.println("Size of deque = "+s);
    }catch(InterruptedException e){
      e.printStackTrace();
    }
  }else{
    System.out.println(name+" inserted elements into deque are :");
    for(i=0;i<5; i++){
      bdq.add(i);
    System.out.println(i);
    try{
    Thread.sleep(1000);
    }
    catch(InterruptedException e){
      e.printStackTrace();
      }
    }
    int s = bdq.size();      
   System.out.println("Size of deque = "+s);
  }
 }
}
class Size1 implements Runnable{
  String name;
  BlockingDeque<Integer> bdq;  
  public Size1(String name,BlockingDeque<Integer> bdq){
    this.name = name;
   this.bdq = bdq;
  }
  public void run(){
    try{
    bdq.removeAll(bdq);
    System.out.println("Elements of deque are = "+bdq);
    System.out.println("Size of deque : "+bdq.size());
    Thread.sleep(6000);
    }
    catch(InterruptedException e){
      e.printStackTrace();
    }
 }
}
public class BdqSize {
  public static void main(String args []){
  BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>(5);
  Runnable a =new Size("A", bdq);
  Runnable b =new Size("B", bdq);
  new Thread(a).start();
  try{
    Thread.sleep(5000);
    }
    catch(InterruptedException e){
      e.printStackTrace();
    }
  new Thread(b).start();
  }
}

Output :

Deque is empty true

A inserted elements into deque are :

0

1

2

3

4

Size of deque = 5

Deque is empty false

Elements of deque are :[0, 1, 2, 3, 4]

Size of deque = 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics