Pagina 1 di 1

Programmazione concorrente java.... Exchanger... chi mi sa aiutare???

Inviato: mercoledì 10 settembre 2008, 18:24
da eMiliu
Ciao ragazzi....

mi trovo a non capire bene.... doc'è che sbaglio....
Questa è la traccia:
In un self service, ognuno dei K utenti dopo aver mangiato, consegna il vassoio vuoto ad un addetto, il cui compito è quello di ricevere quest’ultimo e posizionarlo in un carrello che ha a disposizione N slot per i vassoi (con K>N). Una volta occupate le N posizioni, l’addetto non può più raccogliere vassoi dai clienti fino a quando non ha consegnato il carrello pieno ad un altro addetto che ha il compito di svuotarlo e restituirlo.
Si implementi lo scenario descritto utilizzando i thread e lo strumento di sincronizzazione Exchanger del package java.util.concurrent.
e il mio codice è questo:

Codice: Seleziona tutto

import java.util.*;
import java.util.concurrent.*;

class Addetto1 implements Runnable{
	private int max;
	private List<Integer> currentBuffer;
	private Integer i;

	private Exchanger<Integer> e; // comunica con i clienti
	private Exchanger<List<Integer>> e2; //comunica con l'addetto2

	public Addetto1(int dimCarrello,Exchanger<Integer> e,Exchanger<List<Integer>> e2){
		max = dimCarrello;
		currentBuffer = new LinkedList<Integer>();
		e = e;
		e2 = e2;
	}

	public void run(){
		while(true){
			System.out.println("L'addetto1 è pronto a ricevere i vassoio");
			try{
				i = e.exchange(null);
			}catch(InterruptedException e){ e.printStackTrace();}

			System.out.println("L'addetto1 mette il vassoio nel carrello");
			currentBuffer.add(i);

			try{
				if(currentBuffer.size() == max)
				currentBuffer = e2.exchange(currentBuffer);
			}catch(InterruptedException e){ e.printStackTrace();}
		}
	}
}

class Addetto2 implements Runnable{
	private int dim;
	private List<Integer> currentBuffer;
	private Exchanger<List<Integer>> e2;

	public Addetto2(int dim,Exchanger<List<Integer>> e2){
		this.dim = dim;
		currentBuffer = new LinkedList<Integer>();
		this.e2 = e2;
	}

	public void run(){
		while (true){
			try{
			     currentBuffer =e2.exchange(null);
			}catch(InterruptedException e){ e.printStackTrace();}

			try{
				if(currentBuffer.isEmpty()){
					System.out.println("L'addetto2 porta il carrello all'addetto1");
					currentBuffer = e2.exchange(currentBuffer);
				}

				System.out.println("L'addetto2 prende il carrello lo e lo svuota");
				currentBuffer.clear();
			}catch (InterruptedException ex) { System.out.println(ex); }
		}
	}
}

class Cliente extends Thread{
	private int id;
	private Exchanger<Integer> e;

	public Cliente(int id,Exchanger<Integer> e){
		this.id = id;
		this.e = e;
	}

	public void run(){
		while(true){
			try{
				System.out.println("Il cliente "+id+" porta il vassoio all'addetto1");
				Integer o = e.exchange((Integer)id);
			}catch(InterruptedException e){ System.out.println(e);}
		}
	}
}

public class mainSelf{
	public static void main(String args []){

		int p = 15;
		Exchanger<List<Integer>> exchanger = new Exchanger<List<Integer>>();
		Exchanger<Integer> ex = new Exchanger<Integer>();
		new Thread(new Addetto1(p,ex,exchanger)).start();
		new Thread(new Addetto2(p,exchanger)).start();

		Cliente [] c = new Cliente[100];
		for(int i=0;i<c.length;i++)
			c[i] = new Cliente(i,ex);
		for(int i=0;i<c.length;i++)
			c[i].start();
	}

}
non so dove sbaglio..... e va in loop.... :'(
cerco aiuto!! :(