Skip to content

Commit 51d79e2

Browse files
committed
problemi deadlock
1 parent 833ae07 commit 51d79e2

File tree

21 files changed

+963
-0
lines changed

21 files changed

+963
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package es5.barbiere;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
public class Barbiere extends Thread {
6+
7+
Sala s;
8+
9+
Barbiere(Sala s) {
10+
this.s = s;
11+
}
12+
13+
@Override
14+
public void run() {
15+
try {
16+
s.tagliaCapelli();
17+
taglio();
18+
} catch (InterruptedException e) {
19+
throw new RuntimeException(e);
20+
}
21+
}
22+
23+
private void taglio() throws InterruptedException {
24+
System.out.println("Taglio in corso...");
25+
TimeUnit.SECONDS.sleep(3);
26+
System.out.println("Fine taglio");
27+
}
28+
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package es5.barbiere;
2+
3+
public class Cliente extends Thread {
4+
5+
private Sala s;
6+
private int id;
7+
8+
Cliente(Sala s, int id) {
9+
this.s = s;
10+
this.id = id;
11+
}
12+
13+
@Override
14+
public void run() {
15+
try {
16+
System.out.printf("Cliente %d è arrivato.%n", id);
17+
boolean taglioEffettuato = s.attendiTaglio();
18+
if (taglioEffettuato) {
19+
System.out.printf("Cliente %d ha tagliato i capelli.%n", id);
20+
} else {
21+
System.out.printf("Cliente %d va via.%n", id);
22+
}
23+
} catch (InterruptedException e) {
24+
throw new RuntimeException(e);
25+
}
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package es5.barbiere;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
public abstract class Sala {
6+
7+
protected final int numSedie;
8+
protected int sedieLibere;
9+
10+
public Sala(int sedie){
11+
numSedie = sedie; sedieLibere = sedie;
12+
}
13+
14+
public abstract void tagliaCapelli() throws InterruptedException;
15+
16+
public abstract boolean attendiTaglio() throws InterruptedException;
17+
18+
public void test(int numClienti) throws InterruptedException{
19+
Barbiere b = new Barbiere(this);
20+
b.setDaemon(true);
21+
b.start();
22+
Cliente[] clienti = new Cliente[numClienti];
23+
for(int i = 0; i < numClienti; i++){
24+
clienti[i] = new Cliente(this, i);
25+
clienti[i].start();
26+
TimeUnit.SECONDS.sleep(2);
27+
}
28+
}
29+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package es5.barbiere;
2+
3+
import java.util.concurrent.locks.*;
4+
5+
public class SalaLC extends Sala {
6+
7+
Lock lock = new ReentrantLock();
8+
Condition clienteDisponibile = lock.newCondition();
9+
Condition barbiereLibero = lock.newCondition();
10+
boolean poltronaLibera = false;
11+
12+
public SalaLC(int sedie) {
13+
super(sedie);
14+
}
15+
16+
private int clientiInAttesa() {
17+
return numSedie - sedieLibere;
18+
}
19+
20+
@Override
21+
public void tagliaCapelli() throws InterruptedException {
22+
lock.lock();
23+
try {
24+
while (clientiInAttesa() == 0) {
25+
clienteDisponibile.await();
26+
}
27+
poltronaLibera = true;
28+
barbiereLibero.signal();
29+
} finally {
30+
lock.unlock();
31+
}
32+
}
33+
34+
@Override
35+
public boolean attendiTaglio() throws InterruptedException {
36+
lock.lock();
37+
try {
38+
if (sedieLibere == 0) {
39+
return false;
40+
}
41+
sedieLibere--;
42+
clienteDisponibile.signal();
43+
while (!poltronaLibera) {
44+
barbiereLibero.await();
45+
}
46+
poltronaLibera = false;
47+
sedieLibere++;
48+
return true;
49+
} finally {
50+
lock.unlock();
51+
}
52+
}
53+
54+
public static void main(String[] args) {
55+
SalaLC sala = new SalaLC(10);
56+
try {
57+
sala.test(30);
58+
} catch (InterruptedException e) {
59+
e.printStackTrace();
60+
}
61+
}
62+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package es5.barbiere;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
public class SalaSem extends Sala {
6+
7+
Semaphore mutex = new Semaphore(1);
8+
Semaphore clientiDisponibili = new Semaphore(0, true);
9+
Semaphore poltrona = new Semaphore(0, true);
10+
11+
public SalaSem(int sedie) {
12+
super(sedie);
13+
}
14+
15+
@Override
16+
public void tagliaCapelli() throws InterruptedException {
17+
clientiDisponibili.acquire();
18+
poltrona.release();
19+
}
20+
21+
@Override
22+
public boolean attendiTaglio() throws InterruptedException {
23+
mutex.acquire();
24+
if (sedieLibere == 0) { // se tutto occupato se ne va
25+
mutex.release();
26+
return false;
27+
}
28+
sedieLibere--;
29+
mutex.release();
30+
clientiDisponibili.release();
31+
poltrona.acquire(); // barbiere si "addormenta" sulla poltrona
32+
33+
mutex.acquire();
34+
sedieLibere++; // libera la sedia
35+
mutex.release();
36+
return true;
37+
}
38+
39+
public static void main(String[] args) {
40+
Sala s = new SalaSem(5);
41+
try {
42+
s.test(30);
43+
} catch (InterruptedException e) {e.printStackTrace();}
44+
}
45+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package es5.bounded_buffer;
2+
3+
import java.util.Arrays;
4+
import java.util.concurrent.TimeUnit;
5+
6+
public abstract class Buffer {
7+
8+
protected int[] buffer;
9+
protected int in = 0, out = 0;
10+
11+
Buffer(int capacity) {
12+
buffer = new int[capacity];
13+
}
14+
15+
public int getCapacity() {
16+
return buffer.length;
17+
}
18+
19+
abstract void put(int i) throws InterruptedException;
20+
21+
abstract int get() throws InterruptedException;
22+
23+
public void test(int numProduttori, int numConsumatori) {
24+
Produttore[] p = new Produttore[numProduttori];
25+
Consumatore[] c = new Consumatore[numConsumatori];
26+
27+
for (int i = 0; i < numConsumatori; i++) {
28+
c[i] = new Consumatore(this);
29+
c[i].start();
30+
}
31+
for (int i = 0; i < numProduttori; i++) {
32+
p[i] = new Produttore(this);
33+
p[i].start();
34+
}
35+
36+
try {
37+
TimeUnit.SECONDS.sleep(10);
38+
} catch (InterruptedException e) {
39+
e.printStackTrace();
40+
}
41+
42+
for (int i = 0; i < numProduttori; i++) {
43+
p[i].interrupt();
44+
}
45+
46+
for (int i = 0; i < numConsumatori; i++) {
47+
c[i].interrupt();
48+
}
49+
50+
try {
51+
for (int i = 0; i < numProduttori; i++) {
52+
p[i].join();
53+
}
54+
55+
for (int i = 0; i < numConsumatori; i++) {
56+
c[i].join();
57+
}
58+
} catch (InterruptedException e) {
59+
e.printStackTrace();
60+
}
61+
62+
int total = 0;
63+
for (int i = 0; i < numProduttori; i++) {
64+
total += p[i].n_operations;
65+
}
66+
67+
for (int i = 0; i < numConsumatori; i++) {
68+
total -= c[i].n_operations;
69+
}
70+
71+
System.out.printf("%s: operations' total is %d.%n", total >= 0 ? "Ok": "Error", total);
72+
System.out.println(Arrays.toString(buffer));
73+
}
74+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package es5.bounded_buffer;
2+
3+
import java.util.concurrent.locks.Condition;
4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
public class BufferLC extends Buffer {
8+
9+
private int elements = 0;
10+
private Lock lock = new ReentrantLock();
11+
private Condition full = lock.newCondition();
12+
private Condition empty = lock.newCondition();
13+
14+
15+
BufferLC(int capacity) {
16+
super(capacity);
17+
}
18+
19+
@Override
20+
void put(int i) throws InterruptedException {
21+
lock.lock();
22+
try {
23+
while (elements == buffer.length) {
24+
full.await();
25+
}
26+
27+
buffer[in] = i;
28+
in = (in + 1) % buffer.length;
29+
30+
elements++;
31+
empty.signal();
32+
} finally {
33+
lock.unlock();
34+
}
35+
}
36+
37+
@Override
38+
int get() throws InterruptedException {
39+
lock.lock();
40+
try {
41+
while (elements == 0) {
42+
empty.await();
43+
}
44+
45+
int temp = buffer[out];
46+
out = (out + 1) % buffer.length;
47+
48+
elements--;
49+
full.signal();
50+
return temp;
51+
} finally {
52+
lock.unlock();
53+
}
54+
}
55+
56+
public static void main(String[] args) {
57+
Buffer bufferLC = new BufferLC(12);
58+
bufferLC.test(5, 5);
59+
}
60+
}

0 commit comments

Comments
 (0)