Professional Documents
Culture Documents
LinkedList;
import java.util.concurrent.Semaphore;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.Random;
else {
z = "false";
}
if (b == "go")
return z;
return "w";
}
while (true) {
Thread.sleep(100);
if (list.size() == 3) {
await = barrier.await();
sem.acquire(1);
mutex.acquire();
// System.out.println("[-] empty");
while (list.size() != 0) {
Random rand = new Random();
int z = rand.nextInt(list.size());
if ( getP() == true ) {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
// Thread.sleep(200);
System.exit(1);
}
mutex.release();
// if (await == 0) {
// System.out.println("\tconsum. are done");
send("send");
// }
}
}
} catch (Exception x) {
x.printStackTrace();
}
}
}
while (true) {
mutex.acquire();
// int await = barrier.await();
mutex.release();
sem.release(1);
Thread.sleep(200);
}
} catch (Exception x) {
x.printStackTrace();
}
}
}
initArr (par);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(par.length);
new Producer("1").start();
new Producer("2").start();
new Consumer(barrier,"1").start();
new Consumer(barrier,"2").start();
new Consumer(barrier,"3").start();
new Consumer(barrier,"4").start();
new Consumer(barrier,"5").start();
}
}
import java.util.*;
myStack[semafor++] = ob;
System.out.println("Producatorul " + " a pus: " + ob);
if (semafor == 9) {
contor++;
available=false;
notifyAll();
}
} else {
this.wait();
System.out.println("Stva este plina");
}
}
public synchronized Object pop() throws InterruptedException {
@Override
public void run() {
while (buffer.contor<5) {
@Override
public void run() {
while (true) {
try {
System.out.println("Consumatorul " + this.number + " a luat:
" + buffer.pop());
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
sleep((int) (Math.random() * 100));
} catch (InterruptedException e) {
}
}
}
}
public static void main(String[] args) {
p1.start();
p2.start();
p3.start();
p4.start();
c1.start();
c2.start();
c3.start();
while (true){
if(!p1.isAlive()&&!p2.isAlive()&&!p3.isAlive()&&!p4.isAlive()){
c1.stop();
c2.stop();
c3.stop();
break;
}
}
}
}