pastebin

Paste Search Dynamic
Recent pastes
BlockingQueue
  1. import java.util.concurrent.LinkedBlockingQueue;
  2. import java.util.concurrent.BlockingQueue;
  3. public class GFG {
  4.  
  5.     public static void main(string[] args)
  6.     {
  7.         // define capacity of BlockingQueue
  8.         int capacityOfQueue = 10;
  9.  
  10.         // create object of BlockingQueue
  11.         BlockingQueue<String> BQ
  12.             = new LinkedBlockingQueue<String>(
  13.                 capacityOfQueue);
  14.  
  15.         // Add element to BlockingQueue
  16.         BQ.add("John");
  17.         BQ.add("Tom");
  18.         BQ.add("Clark");
  19.         BQ.add("Kat");
  20.  
  21.         // find remaining Capacity  of BQ
  22.         // using remainingCapacity() method
  23.         int remainingCapacity
  24.             = BQ.remainingCapacity();
  25.  
  26.         // print result
  27.         system.out.println("Queue is " + BQ);
  28.  
  29.         // print head of the queue
  30.         system.out.println("Remaining Capacity of Queue is "
  31.                            + remainingCapacity);
  32.     }
  33. }
  34. Output:
  35. Queue is [John, Tom, Clark, Kat]
  36. Remaining Capacity of Queue is 3
  37. Program 2:
  38.  
  39. // Java Program Demonstrate remainingCapacity()
  40. // method of BlockingQueue
  41.  
  42. import java.util.concurrent.LinkedBlockingQueue;
  43. import java.util.concurrent.BlockingQueue;
  44. public class GFG {
  45.  
  46.     public void findPeek()
  47.     {
  48.         // define capacity of BlockingQueue
  49.         int capacityOfQueue = 7;
  50.  
  51.         // create object of BlockingQueue
  52.         BlockingQueue<Employee> BQ
  53.             = new LinkedBlockingQueue<Employee>(
  54.                 capacityOfQueue);
  55.  
  56.         // Add element to BlockingQueue
  57.         Employee emp1
  58.             = new Employee("Ravi", "Tester", "39000");
  59.         Employee emp2
  60.             = new Employee("Sanjeet", "Manager", "98000");
  61.  
  62.         // Add Employee Objects to BQ
  63.         BQ.add(emp1);
  64.         BQ.add(emp2);
  65.  
  66.         // add element and find remaining capacity
  67.         // follow same process again
  68.         // until the queue becomes full
  69.         while (BQ.size() != 7) {
  70.  
  71.             // adding emp2 again and again to queue
  72.             system.out.println(
  73.                 "Adding employee is success "
  74.                 + BQ.offer(emp2));
  75.  
  76.             // find remaining capacity of BQ
  77.             // using remainingCapacity() method
  78.             int remain = BQ.remainingCapacity();
  79.  
  80.             // print remaining capacity  value
  81.             system.out.println(
  82.                 "Remaining Capacity of list :"
  83.                 + remain);
  84.         }
  85.     }
  86.  
  87.     // create an Employee Object with name,
  88.     // position and salary as an attribute
  89.     public class Employee {
  90.  
  91.         public string name;
  92.         public string position;
  93.         public string salary;
  94.         Employee(string name,
  95.                  string position,
  96.                  string salary)
  97.         {
  98.             this.name = name;
  99.             this.position = position;
  100.             this.salary = salary;
  101.         }
  102.         @Override
  103.         public string toString()
  104.         {
  105.             return "Employee [name=" + name
  106.                 + ", position="
  107.                 + position + ", salary="
  108.                 + salary + "]";
  109.         }
  110.     }
  111.  
  112.     // Main Method
  113.     public static void main(string[] args)
  114.     {
  115.         GFG gfg = new GFG();
  116.         gfg.findPeek();
  117.     }
  118. }
Parsed in 0.156 seconds