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 | package stack_using_arraylist_lock_condition; import java.util.ArrayList; 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 Stack_Using_ArrayList_lock_condition { /** * @param args the command line arguments */ public static void main(String[] args) { StackArrayList stList = new StackArrayList(); stList.push(10); stList.push(20); stList.push(30); stList.push(40); stList.push(50); stList.pop(); //Implemeting with threads StackArrayList sa = new StackArrayList(); Pusher t1 = new Pusher(sa); Pusher t2 = new Pusher(sa); Poper t3 = new Poper(sa); t1.start(); t2.start(); t3.start(); } } class StackArrayList{ public int top; public int maxSize = 10; public ArrayList<Integer> stackList; private final Lock myLock = new ReentrantLock(); private final Condition isEmty__Condition = myLock.newCondition(); private final Condition isFull__Condition = myLock.newCondition(); public StackArrayList() { top = -1; stackList = new ArrayList<Integer>(); } //Science this method is allready avalable //No need to implement. //So commented... lol /* public boolean isEmpty(){ if(this.stackList.isEmpty()){ 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("Puser is waiting now... Array is full.."); isFull__Condition.await(); } top++; System.out.println("Item added..."); stackList.add(val); isEmty__Condition.signalAll(); } catch (InterruptedException ex) { Logger.getLogger(StackArrayList.class.getName()).log(Level.SEVERE, null, ex); } finally{ myLock.unlock(); } } public void pop(){ myLock.lock(); try { while(stackList.isEmpty()){ System.out.println("Poper is wating... Stack is empty.."); isEmty__Condition.await(); } System.out.println("Pop item is : " + stackList.get(top)); stackList.remove(top); top --; isFull__Condition.signalAll(); } catch (InterruptedException ex) { Logger.getLogger(StackArrayList.class.getName()).log(Level.SEVERE, null, ex); } finally{ myLock.unlock(); } } public void dispyaFullArray(){ for(int x: stackList){ System.out.println("Item is : " + x); } } } class Poper extends Thread{ StackArrayList sal; public Poper(StackArrayList sal) { this.sal = sal; } @Override public void run(){ System.out.println("Poper start operation and poping like carazy..."); for(int a=0; a<sal.maxSize-1; a++){ //System.out.println("poping index of " + a ); sal.pop(); } } } class Pusher extends Thread{ StackArrayList sal; Random myRand = new Random(); public Pusher(StackArrayList sal) { this.sal = sal; } @Override public void run(){ System.out.println("Now the puser is pushing like carazy.."); for(int a=0; a<sal.maxSize; a++){ sal.push(myRand.nextInt()); } } } |
EmoticonEmoticon