Skip to content

Commit da72aff

Browse files
committed
traccia da controllare
1 parent ba6ca83 commit da72aff

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package tracce.ufficiopostale;
2+
3+
import java.util.Random;
4+
5+
public class Cliente extends Thread {
6+
7+
UfficioPostale ufficio;
8+
9+
Cliente(UfficioPostale ufficio) {
10+
this.ufficio = ufficio;
11+
}
12+
13+
@Override
14+
public void run() {
15+
try {
16+
Random random = new Random();
17+
String operazione = null;
18+
switch (random.nextInt(3)) {
19+
case 0:
20+
operazione = "A";
21+
break;
22+
case 1:
23+
operazione = "B";
24+
break;
25+
case 2:
26+
operazione = "C";
27+
break;
28+
}
29+
boolean disponibile = ufficio.ritiraTicket(operazione);
30+
if (disponibile) {
31+
ufficio.attendiSportello(operazione);
32+
}
33+
ufficio.log("Arrivederci", -1);
34+
} catch (InterruptedException e) { e.printStackTrace(); }
35+
}
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package tracce.ufficiopostale;
2+
3+
public class Impiegato extends Thread {
4+
5+
UfficioPostale ufficio;
6+
int tipo;
7+
8+
Impiegato(UfficioPostale ufficio, int tipo) {
9+
this.ufficio = ufficio;
10+
this.tipo = tipo;
11+
}
12+
13+
@Override
14+
public void run() {
15+
while (true) {
16+
try {
17+
ufficio.prossimoCliente();
18+
ufficio.eseguiOperazione();
19+
} catch (InterruptedException e) { e.printStackTrace(); }
20+
}
21+
}
22+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package tracce.ufficiopostale;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.TimeUnit;
5+
6+
public abstract class UfficioPostale {
7+
8+
int numeroImpiegatiPerTipo;
9+
10+
UfficioPostale(int numeroImpiegatiPerTipo) {
11+
this.numeroImpiegatiPerTipo = numeroImpiegatiPerTipo;
12+
}
13+
14+
abstract boolean ritiraTicket(String operazione) throws InterruptedException;
15+
16+
abstract void attendiSportello(String operazione) throws InterruptedException;
17+
18+
abstract void prossimoCliente() throws InterruptedException;
19+
20+
abstract void eseguiOperazione() throws InterruptedException;
21+
22+
void attesa(int t) {
23+
try {
24+
TimeUnit.SECONDS.sleep(t);
25+
} catch (InterruptedException e) { e.printStackTrace(); }
26+
}
27+
28+
void attesa(int t1, int t2) {
29+
Random random = new Random();
30+
attesa(random.nextInt(t1, t2));
31+
}
32+
33+
void log(String msg, int tipo) {
34+
String codice = null;
35+
switch (tipo) {
36+
case 0:
37+
codice = "A"; break;
38+
case 1:
39+
codice = "B"; break;
40+
case 2:
41+
codice = "C"; break;
42+
default:
43+
codice = "Cliente"; break;
44+
}
45+
long id = Thread.currentThread().getId();
46+
String finalMsg = String.format("[%d] %s: %s", id, codice, msg);
47+
System.out.println(finalMsg);
48+
}
49+
50+
void test(int numeroClienti) {
51+
for (int j = 0; j < 3; j++) {
52+
for (int i = 0; i < numeroImpiegatiPerTipo; i++) {
53+
new Impiegato(this, j).start();
54+
}
55+
}
56+
for (int i = 0; i < numeroClienti; i++) {
57+
new Cliente(this).start();
58+
}
59+
}
60+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package tracce.ufficiopostale;
2+
3+
import java.util.LinkedList;
4+
import java.util.Map;
5+
import java.util.concurrent.locks.Condition;
6+
import java.util.concurrent.locks.Lock;
7+
import java.util.concurrent.locks.ReentrantLock;
8+
9+
public class UfficioPostaleLC extends UfficioPostale {
10+
11+
int[] durataOperazione = new int[] { 3, 5, 7};
12+
13+
Lock l = new ReentrantLock();
14+
Condition possoRitirare = l.newCondition();
15+
16+
LinkedList<Thread> coda = new LinkedList<>();
17+
int[] ticketEmessi = new int[] { 0, 0, 0 };
18+
final int TICKET_MAX = 50;
19+
20+
Map<Integer, LinkedList<Thread>> sportelli = Map.of(
21+
0, new LinkedList<>(),
22+
1, new LinkedList<>(),
23+
2, new LinkedList<>()
24+
);
25+
Condition turnoSportello = l.newCondition();
26+
boolean[] clienteAlloSportello = new boolean[] { false, false, false };
27+
Condition[] clienteDisponibile = new Condition[] { l.newCondition(), l.newCondition(), l.newCondition() };
28+
29+
boolean[] operazioneTerminata = new boolean[] { false, false, false };
30+
Condition[] termineOperazione = new Condition[] { l.newCondition(), l.newCondition(), l.newCondition() };
31+
32+
UfficioPostaleLC(int numeroImpiegatiPerTipo) {
33+
super(numeroImpiegatiPerTipo);
34+
}
35+
36+
int converti(String operazione) {
37+
Map<String, Integer> op = Map.of(
38+
"A", 0,
39+
"B", 1,
40+
"C", 2
41+
);
42+
return op.get(operazione);
43+
}
44+
45+
@Override
46+
boolean ritiraTicket(String operazione) throws InterruptedException {
47+
l.lock();
48+
try {
49+
coda.add(Thread.currentThread());
50+
while (!Thread.currentThread().equals(coda.getFirst())) {
51+
possoRitirare.await();
52+
}
53+
coda.removeFirst();
54+
int codice = converti(operazione);
55+
if (ticketEmessi[codice] == TICKET_MAX) return false;
56+
ticketEmessi[codice]++;
57+
possoRitirare.signalAll();
58+
return true;
59+
} finally {
60+
l.unlock();
61+
}
62+
}
63+
64+
boolean mioTurno(int codice) {
65+
return Thread.currentThread().equals(sportelli.get(codice).getFirst());
66+
}
67+
68+
@Override
69+
void attendiSportello(String operazione) throws InterruptedException {
70+
l.lock();
71+
try {
72+
int codice = converti(operazione);
73+
while (!mioTurno(codice)) {
74+
turnoSportello.await();
75+
}
76+
clienteAlloSportello[codice] = true;
77+
while (!operazioneTerminata[codice]) {
78+
termineOperazione[codice].await();
79+
}
80+
81+
} finally {
82+
l.unlock();
83+
}
84+
}
85+
86+
@Override
87+
void prossimoCliente() throws InterruptedException {
88+
l.lock();
89+
try {
90+
turnoSportello.signalAll();
91+
} finally {
92+
l.unlock();
93+
}
94+
}
95+
96+
@Override
97+
void eseguiOperazione() throws InterruptedException {
98+
l.lock();
99+
int codice;
100+
try {
101+
codice = ((Impiegato) Thread.currentThread()).tipo;
102+
while (!clienteAlloSportello[codice]) {
103+
clienteDisponibile[codice].await();
104+
}
105+
} finally {
106+
l.unlock();
107+
}
108+
109+
attesa(durataOperazione[codice]);
110+
111+
l.lock();
112+
try {
113+
termineOperazione[codice].signal();
114+
} finally {
115+
l.unlock();
116+
}
117+
}
118+
119+
public static void main(String[] args) {
120+
UfficioPostale ufficio = new UfficioPostaleLC(1);
121+
ufficio.test(200);
122+
}
123+
}

0 commit comments

Comments
 (0)