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 163 164 | package que_using_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 Que_using_lock_condition { /** * @param args the command line arguments */ public static void main(String[] args) { //Testing the class befor thread /* Que q = new Que(); q.push(10); q.push(20); q.push(30); q.push(40); q.push(50); q.pop(); q.pop(); q.pop(); q.pop(); q.pop(); //Now all the valuse are poped out... q.pop(); q.pop(); */ //Now the Thread inplemetation Que tQue = new Que(); Pusher t1 = new Pusher(tQue); Poper t2 = new Poper(tQue); Pusher t3 = new Pusher(tQue); t1.start(); t2.start(); t3.start(); } } class Que{ private static Lock myLock = new ReentrantLock(); private static Condition isEmpty__Condition = myLock.newCondition(); private static Condition isFull__Condition = myLock.newCondition(); public int maxSixe = 10; public int top; public int rear; public int[] que; public Que() { this.top = -1; this.rear = 0; que = new int[maxSixe]; } public boolean isFull(){ if(top == maxSixe-1){ return true; } else{ return false; } } public boolean isEmpty(){ if(top == -1){ return true; } else{ return false; } } public void push(int newVal){ myLock.lock(); try { while(this.isFull()){ System.out.println("Push operation is wating array is full..."); isFull__Condition.await(); } top +=1; que[top] = newVal; System.out.println("Pushing item, top is now: "+top + " New valuse is "+ newVal); isEmpty__Condition.signalAll(); } catch (InterruptedException ex) { Logger.getLogger(Que.class.getName()).log(Level.SEVERE, null, ex); } finally{ myLock.unlock(); } } public void pop(){ myLock.lock(); try { while(this.isEmpty() && rear>=top){ System.out.println("Pop is wating since no items in array..."); isEmpty__Condition.await(); } System.out.println("Item is poped.. Value is :" + que[rear]); rear +=1; isFull__Condition.signalAll(); } catch (InterruptedException ex) { Logger.getLogger(Que.class.getName()).log(Level.SEVERE, null, ex); } finally{ myLock.unlock(); } } } class Poper extends Thread{ Que myQue; public Poper(Que myQue) { this.myQue = myQue; } @Override public void run() { System.out.println("Poper is on action...."); for(int a=0; a<myQue.maxSixe; a++){ myQue.pop(); } } } class Pusher extends Thread{ Que myQue; Random myRand = new Random(); public Pusher(Que myQue) { this.myQue = myQue; } @Override public void run() { System.out.println("Pusher is on action...."); for(int a=0; a<myQue.maxSixe; a++){ myQue.push(myRand.nextInt()); } } } |
EmoticonEmoticon