You are on page 1of 8

import java.util.

LinkedList;
import java.util.concurrent.Semaphore;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.Random;

public class Main {

static Object LOCK = new Object();

static LinkedList list = new LinkedList();


static Semaphore sem = new Semaphore(0);
static Semaphore mutex = new Semaphore(1);

static CyclicBarrier barrier = new CyclicBarrier(2);


static int await = 0;

static boolean b2 = false;

static int[] par = new int[100];

static int cntP = 0;


static public int countP () {
if (cntP == 31 )
b2 = true;
return cntP++;
}

static public int initArr (int arr[]) {


int z = 0;
for (int i = 0; i < 100 ; i++) {
if (i %2 == 0) {
arr[i] = i;
// System.out.println(arr[i]);
}
}
return z;
}

static public synchronized boolean getP () {


if (b2 == true ) {
// System.out.println("````cntP " + cntP);
return true;
}
else return false;
}

static public synchronized String send (String b) {


String z = "false";
if (b == "send")
z = "true";

else {
z = "false";
}

if (b == "go")
return z;

return "w";
}

static class Consumer extends Thread {


String name;
private CyclicBarrier barrier;

public Consumer (CyclicBarrier barrier, String name) {


this.name = name;
this.barrier = barrier;
}

public void run() {


try {

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());

System.out.println("\tConsumer " + name + " got: " + list.get(z) );


list.remove(z);
System.out.println("\t" + list);

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();
}
}
}

static class Producer extends Thread {


private CyclicBarrier barrier;
String name;

public Producer (String name) {


this.name = name;
}

public void run() {


try {
Random rd = new Random();
int rand = 0;
int k = 0;

while (true) {

mutex.acquire();
// int await = barrier.await();

int arr[] = new int[100];


if ( list.size() < 3 || send("go") == "true") {
k = countP() + 1;

int x=(int) (Math.random()*50);


x=x*2;
list.add(x);
System.out.println(k + ") Producer " + name + " put: " + x);
// System.out.print("Producer " + name + " put: " + par[rand]);
System.out.println(list);
send("stop");
// if (k == 33)
// Thread.sleep(20000);

mutex.release();
sem.release(1);
Thread.sleep(200);
}

} catch (Exception x) {
x.printStackTrace();
}
}
}

public static void main(String [] args) {


try { new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); }
catch (Exception e1) { System.out.println(e1); }

initArr (par);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println(par.length);

CyclicBarrier barrier = new CyclicBarrier(2);


CyclicBarrier barrier2 = new CyclicBarrier(3);

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.*;

import static java.lang.Thread.sleep;


public class Semafor1 {

public enum vocale {


a, e, i, o, u
}
private static char Extrage_vocale_random() {
int pick = new Random().nextInt(vocale.values().length);
return vocale.values()[pick].toString().charAt(0);
}

static class Sincronizare {

private Object myStack[];


private static final int max = 150;
private int semafor = 0;
private int contor = 0;
private int increment=0;
private boolean available=true;

public Sincronizare(int size) {


if (size > max) {
myStack = new Object[max];
} else {
myStack = new Object[size];
}
}

public synchronized boolean isEmpty() {


return semafor == 0;
}

public synchronized boolean isFull() {


return semafor == myStack.length;
}

public synchronized void push(Character ob) throws InterruptedException {

if (!isFull() && available){

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 {

if (!isEmpty() && !available) {


return myStack[--semafor];
} else{
if (semafor==0) {
notifyAll();
available = true;
}
wait();return "Stiva este goala";
}
}
}

static class Producator extends Thread {

private Sincronizare buffer;


private int number;

public Producator(Sincronizare c, int number) {


buffer = c;
this.number = number;
}

@Override
public void run() {
while (buffer.contor<5) {

char temp = Extrage_vocale_random();


//System.out.println("Producatorul " + this.number + " a pus: "
+ temp);
try {
buffer.push(temp);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
sleep((int) (Math.random() * 100));
} catch (InterruptedException e) {
}
}
}
}

static class Consumator extends Thread {

private Sincronizare buffer;


private int number;
private int max = 5;

private int nr_voc_consumate = 0;

public Consumator(Sincronizare c, int number) {


buffer = c;
this.number = number;
}

@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) {

Sincronizare c = new Sincronizare(9);

Producator p1 = new Producator(c, 1);


Producator p2 = new Producator(c, 2);
Producator p3 = new Producator(c, 3);
Producator p4 = new Producator(c, 4);

Consumator c1 = new Consumator(c, 1);


Consumator c2 = new Consumator(c, 2);
Consumator c3 = new Consumator(c, 3);

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;
}
}

}
}

You might also like