Skip to content

Commit 02a2b5c

Browse files
committed
traccia completa con variante
1 parent 3855506 commit 02a2b5c

File tree

5 files changed

+216
-10
lines changed

5 files changed

+216
-10
lines changed
Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
11
package tracce.supermercato;
22

3+
import java.util.Random;
4+
import java.util.concurrent.TimeUnit;
5+
36
public abstract class Cassa {
47

5-
abstract void svuotaCarrello(int N);
8+
final int numeroCasse;
9+
10+
public Cassa(int numeroCasse) {
11+
this.numeroCasse = numeroCasse;
12+
}
13+
14+
abstract void svuotaCarrello(int N) throws InterruptedException;
15+
16+
abstract void scansiona() throws InterruptedException;
17+
18+
abstract void paga(int N) throws InterruptedException;
19+
20+
abstract void prossimoCliente() throws InterruptedException;
621

7-
abstract void scansiona();
22+
void attesa(int t) {
23+
try {
24+
TimeUnit.SECONDS.sleep(t);
25+
} catch (InterruptedException e) { e.printStackTrace(); }
26+
}
827

9-
abstract void paga(int N);
28+
void attesa(int t1, int t2) {
29+
Random random = new Random();
30+
attesa(random.nextInt(t1, t2));
31+
}
1032

11-
abstract void prossimoCliente();
33+
void log(String msg) {
34+
long id = Thread.currentThread().getId();
35+
String finalMsg = String.format("[%d] %s", id, msg);
36+
System.out.println(finalMsg);
37+
}
1238

39+
void test(int numeroClienti) {
40+
for (int i = 0; i < numeroCasse; i++) {
41+
new Cassiere(this, i).start();
42+
}
43+
for (int i = 0; i < numeroClienti; i++) {
44+
new Cliente(this).start();
45+
}
46+
}
1347
}
Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,58 @@
11
package tracce.supermercato;
22

3+
import java.util.concurrent.Semaphore;
4+
35
public class CassaSem extends Cassa {
4-
@Override
5-
void svuotaCarrello(int N) {
66

7+
Semaphore mutex = new Semaphore(1);
8+
int prodotti = 0;
9+
Semaphore prossimoCliente = new Semaphore(1);
10+
Semaphore puoPagare = new Semaphore(0);
11+
Semaphore pagato = new Semaphore(0);
12+
Semaphore puoScansionare = new Semaphore(0);
13+
14+
public CassaSem() {
15+
super(1);
716
}
817

918
@Override
10-
void scansiona() {
11-
19+
void svuotaCarrello(int N) throws InterruptedException {
20+
log("Buongiorno");
21+
prossimoCliente.acquire();
22+
log("Svuoto il carrello");
23+
mutex.acquire();
24+
prodotti += N;
25+
mutex.release();
26+
log("Finito di svuotare il carrello");
27+
puoScansionare.release();
1228
}
1329

1430
@Override
15-
void paga(int N) {
31+
void scansiona() throws InterruptedException {
32+
puoScansionare.acquire();
33+
log("Comincio a scansionare...");
34+
attesa(5 * prodotti);
35+
log("Fine scansione, può pagare");
36+
puoPagare.release();
37+
}
1638

39+
@Override
40+
void paga(int N) throws InterruptedException {
41+
log("Attendo di pagare");
42+
puoPagare.acquire();
43+
log("Importo: €" + 5 * N);
44+
pagato.release();
1745
}
1846

1947
@Override
20-
void prossimoCliente() {
48+
void prossimoCliente() throws InterruptedException {
49+
pagato.acquire();
50+
log("Prossimo cliente");
51+
prossimoCliente.release();
52+
}
2153

54+
public static void main(String[] args) {
55+
Cassa cassa = new CassaSem();
56+
cassa.test(3);
2257
}
2358
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package tracce.supermercato;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.concurrent.Semaphore;
6+
7+
public class CassaSem2 extends Cassa {
8+
9+
Semaphore mutex = new Semaphore(1);
10+
int[] prodotti;
11+
Semaphore[] prossimoCliente;
12+
Semaphore[] puoPagare;
13+
Semaphore[] pagato;
14+
Semaphore[] puoScansionare;
15+
int[] personeInFila;
16+
17+
Map<Thread, Integer> clientiCassa = new HashMap<>();
18+
19+
CassaSem2(int numeroCasse) {
20+
super(numeroCasse);
21+
this.prodotti = new int[numeroCasse];
22+
this.prossimoCliente = new Semaphore[numeroCasse];
23+
this.puoPagare = new Semaphore[numeroCasse];
24+
this.pagato = new Semaphore[numeroCasse];
25+
this.puoScansionare = new Semaphore[numeroCasse];
26+
this.personeInFila = new int[numeroCasse];
27+
for (int i = 0; i < numeroCasse; i++) {
28+
prodotti[i] = 0;
29+
prossimoCliente[i] = new Semaphore(1);
30+
puoPagare[i] = new Semaphore(0);
31+
pagato[i] = new Semaphore(0);
32+
puoScansionare[i] = new Semaphore(0);
33+
personeInFila[i] = 0;
34+
}
35+
}
36+
37+
void scegliCassa() {
38+
int ret = 0;
39+
int min = personeInFila[ret];
40+
for (int i = 1; i < numeroCasse; i++) {
41+
if (personeInFila[i] < min) {
42+
min = personeInFila[i];
43+
ret = i;
44+
}
45+
}
46+
clientiCassa.put(Thread.currentThread(), ret);
47+
}
48+
49+
int getCassa() {
50+
return clientiCassa.get(Thread.currentThread());
51+
}
52+
53+
int getCassiere() {
54+
return ((Cassiere) Thread.currentThread()).id;
55+
}
56+
57+
@Override
58+
void svuotaCarrello(int N) throws InterruptedException {
59+
log("Buongiorno");
60+
mutex.acquire();
61+
scegliCassa();
62+
mutex.release();
63+
prossimoCliente[getCassa()].acquire();
64+
log("Svuoto il carrello");
65+
prodotti[getCassa()] += N;
66+
log("Finito di svuotare il carrello");
67+
puoScansionare[getCassa()].release();
68+
}
69+
70+
@Override
71+
void scansiona() throws InterruptedException {
72+
puoScansionare[getCassiere()].acquire();
73+
log("Comincio a scansionare...");
74+
attesa(2 * prodotti[getCassiere()]);
75+
log("Fine scansione, può pagare");
76+
puoPagare[getCassiere()].release();
77+
}
78+
79+
@Override
80+
void paga(int N) throws InterruptedException {
81+
log("Attendo di pagare");
82+
puoPagare[getCassa()].acquire();
83+
log("Importo: €" + 5 * N);
84+
pagato[getCassa()].release();
85+
}
86+
87+
@Override
88+
void prossimoCliente() throws InterruptedException {
89+
pagato[getCassiere()].acquire();
90+
log("Prossimo cliente");
91+
prossimoCliente[getCassiere()].release();
92+
}
93+
94+
public static void main(String[] args) {
95+
Cassa cassa = new CassaSem2(2);
96+
cassa.test(5);
97+
}
98+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
package tracce.supermercato;
22

33
public class Cassiere extends Thread {
4+
5+
Cassa cassa;
6+
int id;
7+
8+
Cassiere(Cassa cassa, int id) {
9+
this.cassa = cassa;
10+
this.id = id;
11+
}
12+
13+
@Override
14+
public void run() {
15+
while (true) {
16+
try {
17+
cassa.scansiona();
18+
cassa.prossimoCliente();
19+
} catch (InterruptedException e) { e.printStackTrace(); }
20+
}
21+
}
422
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
package tracce.supermercato;
22

3+
import java.util.Random;
4+
35
public class Cliente extends Thread {
6+
7+
Cassa cassa;
8+
9+
Cliente(Cassa cassa) {
10+
this.cassa = cassa;
11+
}
12+
13+
@Override
14+
public void run() {
15+
try {
16+
Random random = new Random();
17+
int N = random.nextInt(1, 21);
18+
cassa.log("Faccio la spesa... " + N);
19+
cassa.attesa(2 * N);
20+
cassa.svuotaCarrello(N);
21+
cassa.paga(N);
22+
cassa.log("Arrivederci");
23+
} catch (InterruptedException e) { e.printStackTrace(); }
24+
}
425
}

0 commit comments

Comments
 (0)