1 | /**
|
---|
2 | * Describe class PC here.
|
---|
3 | *
|
---|
4 | *
|
---|
5 | * Created: Fri Jun 8 14:32:29 2007
|
---|
6 | *
|
---|
7 | * @author <a href="mailto:mattia.monga@unimi.it">Mattia Monga</a>
|
---|
8 | * @version 1.0
|
---|
9 | */
|
---|
10 | import java.util.concurrent.locks.*;
|
---|
11 |
|
---|
12 | class Actor extends Thread
|
---|
13 | {
|
---|
14 | public Actor(String nome){
|
---|
15 | super(nome);
|
---|
16 | }
|
---|
17 |
|
---|
18 | private Magazzino shared;
|
---|
19 | public final Magazzino getShared() {
|
---|
20 | return shared;
|
---|
21 | }
|
---|
22 | public final void setShared(final Magazzino newShared) {
|
---|
23 | this.shared = newShared;
|
---|
24 | }
|
---|
25 |
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | class Produttore extends Actor {
|
---|
30 | public Produttore(String nome, Magazzino b) {
|
---|
31 | super(nome);
|
---|
32 | setShared(b);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void run(){
|
---|
36 | int i = 0;
|
---|
37 | while(true){
|
---|
38 | System.out.println(getName() + ": Inserisco " + i + " nel buffer");
|
---|
39 | getShared().put(i);
|
---|
40 | i += 1;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | class Consumatore extends Actor{
|
---|
46 | public Consumatore(String nome, Magazzino b) {
|
---|
47 | super(nome);
|
---|
48 | setShared(b);
|
---|
49 | }
|
---|
50 | public void run(){
|
---|
51 | while(true){
|
---|
52 | int i = getShared().get();
|
---|
53 | System.out.println(getName() + ": Estraggo " + i + " dal buffer");
|
---|
54 |
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | class Magazzino{
|
---|
60 | public final static int SIZE = 10;
|
---|
61 | private int[] memory = new int[SIZE];
|
---|
62 | private int quanti = 0;
|
---|
63 |
|
---|
64 | public final int get() {
|
---|
65 | monitor.lock();
|
---|
66 | int ris = -1;
|
---|
67 | try{
|
---|
68 | String n = Thread.currentThread().getName();
|
---|
69 | while (isVuoto()){
|
---|
70 | System.out.println(n + " ha tentato di leggere");
|
---|
71 | empty.await();
|
---|
72 | }
|
---|
73 | ris = memory[--quanti];
|
---|
74 | if (quanti == SIZE - 1) full.signal();
|
---|
75 | System.out.println(n + " ha letto");
|
---|
76 | } catch (InterruptedException e){
|
---|
77 | System.err.println(e);
|
---|
78 | } finally {
|
---|
79 | monitor.unlock();
|
---|
80 | return ris;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public final void put(final int newMemory) {
|
---|
85 | monitor.lock();
|
---|
86 | try{
|
---|
87 | String n = Thread.currentThread().getName();
|
---|
88 | while (isPieno()){
|
---|
89 | System.out.println(n + " ha tentato di scrivere");
|
---|
90 | full.await();
|
---|
91 | }
|
---|
92 | memory[quanti++] = newMemory;
|
---|
93 | if (quanti == 1) empty.signal();
|
---|
94 | System.out.println(n + " ha scritto");
|
---|
95 | } catch (InterruptedException e){
|
---|
96 | System.err.println(e);
|
---|
97 | } finally {
|
---|
98 | monitor.unlock();
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public final boolean isVuoto() {
|
---|
103 | return quanti == 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public final boolean isPieno() {
|
---|
107 | return quanti == SIZE;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | private static final Lock monitor = new ReentrantLock();
|
---|
112 | private static final Condition empty = monitor.newCondition();
|
---|
113 | private static final Condition full = monitor.newCondition();
|
---|
114 |
|
---|
115 |
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | public class PCMon {
|
---|
120 |
|
---|
121 | public static final void main(final String[] args) {
|
---|
122 | Magazzino x = new Magazzino();
|
---|
123 | Produttore a1 = new Produttore("Aldo", x);
|
---|
124 | Produttore a2 = new Produttore("Alberto", x);
|
---|
125 | Consumatore b = new Consumatore("Barbara", x);
|
---|
126 | a1.start();
|
---|
127 | b.start();
|
---|
128 | a2.start();
|
---|
129 | }
|
---|
130 |
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|