Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ESEMPIO LOTTERIA */
- /* Classe Giocatore */
- public class Giocatore extends Thread {
- private Lotteria l;
- private int p;
- public Giocatore(Lotteria l, int p) {
- this.l = l;
- this.p = p;
- }
- public void run() {
- try {
- Biglietto b = l.prendiNumero(p);
- boolean ris = l.attendiEstrazione(new Biglietto[]{b});
- System.out.println(ris?"Ho vinto":"Non ho vinto");
- } catch(NumeroPresoException npe) {
- System.out.println("Gia' preso");
- } catch(InterruptedException ie) {
- System.out.println("Interrotto");
- }
- }
- }
- /* Classe Lotteria */
- public class Lotteria {
- private Biglietto[] numero;
- private int venduti;
- private Biglietto estratto;
- private int n;
- public Lotteria(int n) {
- numero = new Biglietto[n];
- this.n = n;
- }
- public synchronized Biglietto prendiNumero(int p) throws NumeroPresoException {
- if(p<0 || p>=n)
- throw new IllegalArgumentException();
- if(numero[p] != null)
- throw new NumeroPresoException();
- Biglietto t = new Biglietto();
- numero[p] = t;
- venduti++;
- return t;
- }
- public synchronized boolean attendiEstrazione(Biglietto[] bb) throws InterruptedException {
- if(venduti == n) {
- estrai();
- notifyAll();
- }
- while(venduti != n)
- wait();
- for(int i=0; i<bb.length; i++)
- if(bb[i]==estratto)
- return true;
- return false;
- }
- private void estrai() {
- int t = (int)(Math.random()*n);
- estratto = numero[t];
- }
- }
- /* Classe Biglietto */
- public class Biglietto {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement