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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | package stackusing_lock_condition; import java.util.Random; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Mahi */ public class StackUsing_lock_condition { /** * @param args the command line arguments */ public static void main(String[] args) { //Testing with push, pops and dispya /* stack stk = new stack(); stk.push(10); stk.push(20); stk.push(30); stk.push(40); stk.displayArray(); */ stack st = new stack(); pusher t1 = new pusher(st); poper t2 = new poper(st); t1.start(); t2.start(); } } class stack{ private final Lock myLock = new ReentrantLock(); private final Condition isFull__Condition = myLock.newCondition(); private final Condition isEmptu_Condition = myLock.newCondition(); public int maxSize = 10; public int stackArr[]; public int top; public stack(){ stackArr = new int[maxSize]; 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 void push(int val){ myLock.lock(); try { while(this.isFull()){ System.out.println("Now push operation is wating.. " + "Arra is full"); isFull__Condition.await(); } top +=1; stackArr[top] = val; System.out.println("Item pushed... Index ; "+ top + " valuse : "+ val); isEmptu_Condition.signalAll(); } catch (InterruptedException ex) { Logger.getLogger(stack.class.getName()).log(Level.SEVERE, null, ex); } finally{ myLock.unlock(); } } public void pop(){ myLock.lock(); try { while(this.isEmpty()){ isEmptu_Condition.await(); } top --; System.out.println("One item poped.. top valuse is :"+ top + "Iten valuse is -: " + stackArr[top]); isFull__Condition.signalAll(); } catch (InterruptedException ex) { Logger.getLogger(stack.class.getName()).log(Level.SEVERE, null, ex ); } finally{ myLock.unlock(); } } public void top(){ System.out.println("Top value is : "+ top + "item is : " + stackArr[top]); } public void displayArray(){ System.out.println("printing item : " + stackArr.toString()); } } class poper extends Thread{ stack myStack; public poper(stack st){ this.myStack = st; } @Override public void run(){ System.out.println("Poper start operation and poping like carazy..."); for(int a=0; a<myStack.maxSize-1; a++){ //System.out.println("poping index of " + a ); myStack.pop(); } } } class pusher extends Thread{ stack myStack; private static Random myRand = new Random(); public pusher(stack st){ this.myStack = st; } @Override public void run(){ System.out.println("Now the puser is pushing like carazy.."); for(int a=0; a<myStack.maxSize; a++){ myStack.push(myRand.nextInt()); } } } |
EmoticonEmoticon