Stack 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
128
129
package stackusing_synchonizedmethod;

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

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

   
    public static void main(String[] args) {
        
        Stack myStack = new Stack();
        Poper t1 = new Poper(myStack);
        Pusher t2 = new Pusher(myStack);
        
        //myStack.push(10);
        //myStack.push(20);
        //t2.setPriority(10);
        //t1.setPriority(5);
  
        t1.start();
        t2.start();
        
        
    }
    
}
class Stack{
    public int maxSize;
    public int[] stackArr;
    public int top;

    public Stack() {
        this.maxSize = 10;
        this.stackArr = new int[maxSize];
        this.top = -1;
    }
    
    public boolean isEmpty(){
        if(top == -1){
            return true;
        }else {
            return false;
        }
    }
    public boolean isFull(){
        if(top == maxSize-1){
            return true;
        }else{
            return false;
        }
    }
    
    public synchronized void push(int val){
        
        try {
            while(this.isFull()){
                System.out.println("Stack is Full... Waiting....");
                wait();
            }
            top +=1;
            stackArr[top] = val;
            System.out.println("One item added to statck. Value : "
                                                         +stackArr[top]);
            notifyAll();
        } 
        catch (InterruptedException ex) {
            Logger.getLogger(Stack.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public synchronized void pop(){
        
        try{
                while(isEmpty()){
                    System.out.println("Now the pop operation is waiting... "
                            + "No items to pop..");
                wait();
                }
                System.out.println("Item is poped.. "+ stackArr[top]);
                top -=1;
        } 
        catch (InterruptedException ex) {
            Logger.getLogger(Stack.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void peek(){
        System.out.println("Peek method return top value, "
                + "The top is " + stackArr[top]);
    }
}

class Pusher extends Thread{
    Stack st;
    Random myRand = new Random();
    
    public Pusher(Stack st) {
        this.st = st;
    }

    @Override
    public void run() {
        for(int a=0; a<st.maxSize; a++){
            st.push(myRand.nextInt(100));
        }
    }
}

class Poper extends Thread{
    Stack st;

    public Poper(Stack st) {
        this.st = st;
    }

    @Override
    public void run() {
        for(int a=0; a<st.maxSize; a++){
            System.out.println("Poper is on operation...");
            st.pop();
        }
    }
}


EmoticonEmoticon