Problema Producatorului Si A Consumatorului

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

Problema producatorului si a consumatorului

class MyData {
private int Data;
public void store(int Data) {
this.Data=Data; }
public int load() {
return this.Data; } }
• class Main{
• public static void main(String argv[]) {
• MyData data = new MyData();
• new Thread(new Producer(data)).start();
• new Thread(new Consumer(data)).start(); } }
• class Producer implements Runnable {
• //Thread-ul - Producator
• MyData data;
• public Producer(MyData data) {
• this.data=data; }

continuare
• public void run() {
• int i;
• for (i=0;;i++) {
• data.store(i);
• System.out.println ("Producer: "+i);
• try { // "adormire" de 0 pina la 0.5 sec
• Thread.sleep ((int) (Math.random()*500));
• } catch (InterruptedException e) { } } } }
• class Consumer implements Runnable {
• MyData data;
• public Consumer(MyData data) {
• this.data=data; }
• public void run() {
• for (;;) {
• System.out.println ("Consumer: "+data.load());
• try {
• Thread.sleep ((int) (Math.random()*500));
• } catch (InterruptedException e) { } } } }
Rezultatul
• Producer: 0
• Consumer: 0
• Producer: 1
• Consumer: 1
• Consumer: 1
• Producer: 2
• Producer: 3
• Consumer: 3
• Producer: 4
• Producer: 5
• Consumer: 5
• Producer: 6
• Consumer: 6
• Producer: 7
• Consumer: 7
• Consumer: 7
• Producer: 8
• Producer: 9
• Producer: 10
• Consumer: 10
Folosirea variabeliior binare
• class MyData {
• private int Data;
• private boolean Ready;
• private boolean Taken;
• public MyData() {
• Ready=false;
• Taken=true; }
• public void store(int Data) {
• while (!Taken);
• this.Data=Data;
• Taken=false;
• Ready=true;
• }

continuare

• public int load() {


• int Data;
• while (!Ready);
• Data = this.Data; //Salvare, deoarece dupa ce
• //Taken devine true, Data se poate schimba oricand
• Ready=false;
• Taken=true;
• return Data;
• }
• }
Sincronizarea utilizand monitoare
• class MyData {
• private int Data;
• private boolean Ready;
• private boolean Taken;
• public MyData() {
• Ready=false;
• Taken=true; }
• public synchronized void store(int Data) {
• while (!Taken);
• this.Data=Data;
• Taken=false;
• Ready=true;
• }
continuare

• public synchronized int load() {


• while (!Ready);
• Ready=false;
• Taken=true;
• return this.Data;
• }
• }
synchronized pentru un segment de cod

• class MyData {
• private int Data;
• private boolean Ready;
• private boolean Taken;
• public MyData() {
• Ready=false;
• Taken=true; }
• public void store(int Data) {
• while (!Taken);
• synchronized (this) {
• this.Data=Data;
• Taken=false;
• Ready=true;
• }
• }
continuare

• public int load() {


• while (!Ready);
• synchronized (this) {
• Ready=false;
• Taken=true;
• return this.Data;
• }
• }
• }
Asteptarea de evenimente. Metodele wait() si notify()

• class MyData {
• private int Data;
• private boolean Ready;
• public MyData() {
• Ready=false;
• }
• public synchronized void store(int Data) {
• while (Ready)
• try {
• wait();
• } catch (InterruptedException e) { }
• this.Data=Data;
• Ready=true;
• notify();
• }
continuare

• public synchronized int load() {


• while (!Ready)
• try {
• wait();
• } catch (InterruptedException e) { }
• Ready=false;
• notify();
• return this.Data; } }
Bariere

• import java.util.*;
• class Barrier { // Clasa Barrier sincronizeaza toti
• //participantii private
• int ParticipatingThreads;
• private int WaitingAtBarrier;
• public Barrier(int num){ //Constructorul
obiectului
• ParticipatingThreads = num;
• WaitingAtBarrier=0;
• }
continuare

• public synchronized void Reached() { //Metoda bariera


• WaitingAtBarrier++;
• if ( ParticipatingThreads != WaitingAtBarrier ) { //Inseamna ca
thread-ul nu este ultimul
• try {
• wait(); //Thread-ul este oprit pina ce
• //este eliberat
• } catch (InterruptedException e) { } } else { // Acesta a fost ultimul
thread activ
• notifyAll();
• WaitingAtBarrier=0; // le elibereaza pe toate
• }
• }
• }
Asteptarea terminarii unui thread

• Class MainThread extends Thread


• {
• public void run()
• {
• SecondaryThread s = new SecondaryThread();
• s.start();
• if (s.isAlive())
• s.join();
• }
• }
Semafoare
• class Semaphore
• {
• protected int value;
• Semaphore( int initialValue )
• {
• value = initialValue; }
• Semaphore()
• {
• value = 0; }
• public synchronized void Get()
• {
• while (value<1)
• wait();
• value--; }
• public synchronized void Put()
• {
• value++;
• notify();
• }}
Exemplu de realuzare
• public class lab22pc {
•   public static void main(String[] args) {
• Depozit depozit = new Depozit();
• Producator p1 = new Producator(depozit, "Producator1");
• Producator p2 = new Producator(depozit, "Producator2");
• Producator p3 = new Producator(depozit, "Producator3");
• Producator p4 = new Producator(depozit, "Producator4");
• Consumator c1 = new Consumator(depozit, "Consumator1");
• Consumator c2 = new Consumator(depozit, "Consumator2");
• Consumator c3 = new Consumator(depozit, "Consumator3");
• p1.start(); p2.start(); p3.start(); p4.start(); c1.start(); c2.start();
• c3.start();
• while(true){
• if(!c1.isAlive()|| !c2.isAlive() || !c3.is Alive() ){
• p1.stop(); p2.stop(); p3.stop(); p4.stop();
• break;
• }} }}
continuare
• class Consumator extends Thread{
• private Depozit d;
• private String nume;
• Consumator(Depozit d1, String n){
• d = d1;
• nume = n; }
• public synchronized void run(){
• int temp; int nr = 3;
• for(int i=0; i < nr; i++){
• temp = d.get(nume);
• if(i == nr-1){
• System.out.println(""+nume+" a consumat "+nr+" elemente"); }
• try{
• sleep((int)(Math.random()*1000));
• }catch(InterruptedException e){}
• } }}
continuare
• class Producator extends Thread{
• private Depozit d;
• private String nume;
• private char[] tab = new char[]{'a','e','i','o','u'};
• Producator(Depozit d1, String n){
• d = d1; nume = n; }
• public synchronized void run(){
• int temp1, temp2;
• while(true){
• temp1 = (int)(Math.random()*5);
• temp2 = (int)(Math.random()*5);
• d.put(tab[temp1], tab[temp2], nume);
• try{
• sleep((int)(Math.random()*1000));
• }catch(InterruptedException e){}
• }}}
continuare
• class Depozit{
• char[] dep = new char[11];
• int c = 0;
• boolean liber = false; boolean gol = false;
• public synchronized int get(String nume){
• while(!liber){
• try{
• wait();
• }catch(InterruptedException e){} }
• System.out.println(""+nume+" a consumat "+dep[c]);
• c--;
• if(c == 0){
• liber = false;
• System.out.println("Depozitul este gol"); }
• if(c < 10){
• gol = false; }
• notifyAll();
• return dep[c+1]; }
continuare
• public synchronized void put(char n1, char n2, String nume){
• while(gol){
• try{
• wait();
• }catch(InterruptedException e){ }
• if(c == 9){ c++; dep[c] = n1;
• System.out.println(""+nume+" a produs "+n1);
• gol = true; liber = true;
• System.out.println("Depozitul este plin"); }
• else{if(c == 8){
• c++; dep[c] = n1;
• c++; dep[c] = n2;
• System.out.println(""+nume+" a produs "+n1+" si "+n2);
• gol = true; liber = true;
• System.out.println("Depozitul este plin"); }
• else if(c < 8){ c++; dep[c] = n1; c++; dep[c] = n2;
• System.out.println(""+nume+" a produs "+n1+" si "+n2);
• gol = false; liber = true } notifyAll(); } }}

You might also like