Queue Using Synchonized Method

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package queusing_synchonizedmethod;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mahi
 */
public class QueUsing_synchonizedmethod {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Que myQue = new Que();
        
        Pusher pT1 = new Pusher(myQue);
        Pusher pT3 = new Pusher(myQue);
        Poper PT2 = new Poper(myQue);
        
        pT1.start();
        PT2.start();
    }
    
}

class Que{
    public int maxSize;
    private int queArr[];
    private int top;
    private int rear;

    public Que() {
        maxSize = 10;
        queArr = new int[maxSize];
        top = -1;
        rear = 0;
    }
    
    public boolean isFull(){
        if(rear >= top){
            return true;
        }
        else return false;
    }
    public boolean isEmpty(){
        if(top == -1){
            return true;
        }
        else return false;
    }
    
    public synchronized void push(int item){
        
        try {
            while(isFull()){
                System.out.println("Now the push operation is waiting, "
                        + "Que is full");
                wait();
            }
            top+=1;
            queArr[top] = item;
            notifyAll();
        } catch (InterruptedException ex) {
            Logger.getLogger(Que.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public synchronized void pop(){    
        try {
             while(isEmpty() && rear>=top){
                System.out.println("Pop operation is wating.. Que is empty");
                wait();
             }
             System.out.println("poped item is : "+ queArr[rear]);
             rear +=1; 
        } catch (InterruptedException ex) {
            Logger.getLogger(Que.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }
    
    public void peek(){
        if(!isEmpty()){
            System.out.println("Peek is : "+ queArr[rear]);
        }
    }
}

class Pusher extends Thread{

    Que que;
    Random myRand = new Random();
            

    public Pusher(Que que) {
        this.que = que;
    }

    @Override
    public void run() {
        System.out.println("Pusher start operation...");
        for(int a=10; a<que.maxSize; a++){
            int rand = myRand.nextInt(100);
            System.out.println("Adding item.." + rand);
            que.push(rand);
        }
    }   
}

class Poper extends Thread{
    Que que;

    public Poper(Que que) {
        this.que = que;
    }

    @Override
    public void run() {
        System.out.println("Poper start operation...");
        for(int a=0; a<que.maxSize; a++){
            System.out.println("Poping item : ");
            que.pop();
        }
    }
}


EmoticonEmoticon