| 905 | * Produttore e consumatore con semafori `PCSem.java` |
| 906 | |
| 907 | {{{ |
| 908 | #!java |
| 909 | import java.util.concurrent.Semaphore; |
| 910 | |
| 911 | class Actor extends Thread |
| 912 | { |
| 913 | public Actor(String nome){ |
| 914 | super(nome); |
| 915 | } |
| 916 | |
| 917 | private Magazzino shared; |
| 918 | public final Magazzino getShared() { |
| 919 | return shared; |
| 920 | } |
| 921 | public final void setShared(final Magazzino newShared) { |
| 922 | this.shared = newShared; |
| 923 | } |
| 924 | |
| 925 | } |
| 926 | |
| 927 | |
| 928 | class Produttore extends Actor { |
| 929 | public Produttore(String nome, Magazzino b) { |
| 930 | super(nome); |
| 931 | setShared(b); |
| 932 | } |
| 933 | |
| 934 | public void run(){ |
| 935 | int i = 0; |
| 936 | while(true){ |
| 937 | System.out.println(getName() + ": Inserisco " + i + " nel buffer"); |
| 938 | getShared().put(i); |
| 939 | i += 1; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | class Consumatore extends Actor{ |
| 945 | public Consumatore(String nome, Magazzino b) { |
| 946 | super(nome); |
| 947 | setShared(b); |
| 948 | } |
| 949 | public void run(){ |
| 950 | while(true){ |
| 951 | int i = getShared().get(); |
| 952 | System.out.println(getName() + ": Estraggo " + i + " dal buffer"); |
| 953 | |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | class Magazzino{ |
| 959 | public final static int SIZE = 10; |
| 960 | private int[] memory = new int[SIZE]; |
| 961 | private int quanti = 0; |
| 962 | |
| 963 | public final int get() { |
| 964 | try{ |
| 965 | String n = Thread.currentThread().getName(); |
| 966 | pieno.acquire(); |
| 967 | mutex.acquire(); |
| 968 | int ris = memory[--quanti]; |
| 969 | mutex.release(); |
| 970 | vuoto.release(); |
| 971 | |
| 972 | System.out.println(n + " ha letto"); |
| 973 | return ris; |
| 974 | } |
| 975 | catch(InterruptedException e){ |
| 976 | System.err.println(e); |
| 977 | return -1; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | public final void put(final int newMemory) { |
| 982 | try{ |
| 983 | String n = Thread.currentThread().getName(); |
| 984 | vuoto.acquire(); |
| 985 | mutex.acquire(); |
| 986 | memory[quanti++] = newMemory; |
| 987 | mutex.release(); |
| 988 | pieno.release(); |
| 989 | System.out.println(n + " ha scritto"); |
| 990 | } |
| 991 | catch(InterruptedException e){ |
| 992 | System.err.println(e); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | private static final Semaphore mutex = new Semaphore(1); |
| 997 | private static final Semaphore pieno = new Semaphore(0); |
| 998 | private static final Semaphore vuoto = new Semaphore(SIZE); |
| 999 | |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | public class PCSem { |
| 1004 | |
| 1005 | public static final void main(final String[] args) { |
| 1006 | Magazzino x = new Magazzino(); |
| 1007 | Produttore a1 = new Produttore("Aldo", x); |
| 1008 | Produttore a2 = new Produttore("Alberto", x); |
| 1009 | Consumatore b = new Consumatore("Barbara", x); |
| 1010 | a1.start(); |
| 1011 | b.start(); |
| 1012 | a2.start(); |
| 1013 | } |
| 1014 | |
| 1015 | } |
| 1016 | }}} |
| 1017 | |
| 1018 | * Produttore e consumatore con monitor `PCMon.java` |
| 1019 | |
| 1020 | {{{ |
| 1021 | #!java |
| 1022 | import java.util.concurrent.locks.*; |
| 1023 | |
| 1024 | class Actor extends Thread |
| 1025 | { |
| 1026 | public Actor(String nome){ |
| 1027 | super(nome); |
| 1028 | } |
| 1029 | |
| 1030 | private Magazzino shared; |
| 1031 | public final Magazzino getShared() { |
| 1032 | return shared; |
| 1033 | } |
| 1034 | public final void setShared(final Magazzino newShared) { |
| 1035 | this.shared = newShared; |
| 1036 | } |
| 1037 | |
| 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | class Produttore extends Actor { |
| 1042 | public Produttore(String nome, Magazzino b) { |
| 1043 | super(nome); |
| 1044 | setShared(b); |
| 1045 | } |
| 1046 | |
| 1047 | public void run(){ |
| 1048 | int i = 0; |
| 1049 | while(true){ |
| 1050 | System.out.println(getName() + ": Inserisco " + i + " nel buffer"); |
| 1051 | getShared().put(i); |
| 1052 | i += 1; |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | class Consumatore extends Actor{ |
| 1058 | public Consumatore(String nome, Magazzino b) { |
| 1059 | super(nome); |
| 1060 | setShared(b); |
| 1061 | } |
| 1062 | public void run(){ |
| 1063 | while(true){ |
| 1064 | int i = getShared().get(); |
| 1065 | System.out.println(getName() + ": Estraggo " + i + " dal buffer"); |
| 1066 | |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | class Magazzino{ |
| 1072 | public final static int SIZE = 10; |
| 1073 | private int[] memory = new int[SIZE]; |
| 1074 | private int quanti = 0; |
| 1075 | |
| 1076 | public final int get() { |
| 1077 | monitor.lock(); |
| 1078 | int ris = -1; |
| 1079 | try{ |
| 1080 | String n = Thread.currentThread().getName(); |
| 1081 | while (isVuoto()){ |
| 1082 | System.out.println(n + " ha tentato di leggere"); |
| 1083 | empty.await(); |
| 1084 | } |
| 1085 | ris = memory[--quanti]; |
| 1086 | if (quanti == SIZE - 1) full.signal(); |
| 1087 | System.out.println(n + " ha letto"); |
| 1088 | } catch (InterruptedException e){ |
| 1089 | System.err.println(e); |
| 1090 | } finally { |
| 1091 | monitor.unlock(); |
| 1092 | return ris; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | public final void put(final int newMemory) { |
| 1097 | monitor.lock(); |
| 1098 | try{ |
| 1099 | String n = Thread.currentThread().getName(); |
| 1100 | while (isPieno()){ |
| 1101 | System.out.println(n + " ha tentato di scrivere"); |
| 1102 | full.await(); |
| 1103 | } |
| 1104 | memory[quanti++] = newMemory; |
| 1105 | if (quanti == 1) empty.signal(); |
| 1106 | System.out.println(n + " ha scritto"); |
| 1107 | } catch (InterruptedException e){ |
| 1108 | System.err.println(e); |
| 1109 | } finally { |
| 1110 | monitor.unlock(); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | public final boolean isVuoto() { |
| 1115 | return quanti == 0; |
| 1116 | } |
| 1117 | |
| 1118 | public final boolean isPieno() { |
| 1119 | return quanti == SIZE; |
| 1120 | } |
| 1121 | |
| 1122 | |
| 1123 | private static final Lock monitor = new ReentrantLock(); |
| 1124 | private static final Condition empty = monitor.newCondition(); |
| 1125 | private static final Condition full = monitor.newCondition(); |
| 1126 | |
| 1127 | |
| 1128 | } |
| 1129 | |
| 1130 | |
| 1131 | public class PCMon { |
| 1132 | |
| 1133 | public static final void main(final String[] args) { |
| 1134 | Magazzino x = new Magazzino(); |
| 1135 | Produttore a1 = new Produttore("Aldo", x); |
| 1136 | Produttore a2 = new Produttore("Alberto", x); |
| 1137 | Consumatore b = new Consumatore("Barbara", x); |
| 1138 | a1.start(); |
| 1139 | b.start(); |
| 1140 | a2.start(); |
| 1141 | } |
| 1142 | |
| 1143 | } |
| 1144 | }}} |