SHOW:
|
|
- or go back to the newest paste.
1 | /* In Java, con i Monitor | |
2 | ||
3 | Realizzare una pipeline (catena di montaggio) di N thread/processi. | |
4 | Passare ai thread/processi una funzione f(i) (immagino | |
5 | la funzione x=x+1) da applicare ai dati secondo lo schema: | |
6 | ||
7 | ||
8 | Se f(i) è la funzione passata di thread T(i) il risultato sarà: */ | |
9 | ||
10 | ||
11 | public class CreaNThread extends Thread { | |
12 | static final int NTHREAD=10; // numero di thread da creare | |
13 | - | static final int NTHREAD=10; // numero di thread da creare |
13 | + | static final int MAX=4; |
14 | - | final int index; // indice del thread appena creato |
14 | + | final int index; |
15 | Pipeline p; | |
16 | - | // costruttore: memorizza l'indice del thread |
16 | + | |
17 | - | CreaNThread(int i) { |
17 | + | CreaNThread(int i,Pipeline pipe) { |
18 | - | index = i; |
18 | + | index = i; p=pipe; |
19 | - | } |
19 | + | } |
20 | ||
21 | - | //codice da eseguire allo start del thread |
21 | + | public void run(){ |
22 | - | public void run(){ |
22 | + | p.threadBody(index); |
23 | - | Pipeline.threadBody(index); |
23 | + | } |
24 | - | } |
24 | + | |
25 | - | |
25 | + | public static void main(String args[]) throws InterruptedException { |
26 | - | |
26 | + | int i; |
27 | Pipeline pipe= new Pipeline(MAX); | |
28 | Thread t[] = new Thread[NTHREAD]; | |
29 | // crea 10 thread e li esegue | |
30 | - | //il main crea i thread, attende la terminazione e stampa il valore finale |
30 | + | for(i=0;i<NTHREAD;i++) { |
31 | - | public static void main(String args[]) throws InterruptedException { |
31 | + | t[i] = new CreaNThread(i,pipe); |
32 | - | int i; |
32 | + | t[i].start(); |
33 | - | Thread t[] = new Thread[NTHREAD]; |
33 | + | } |
34 | ||
35 | - | // crea 10 thread e li esegue |
35 | + | // attende la terminazione dei thread |
36 | for(i=0;i<NTHREAD;i++) { | |
37 | - | t[i] = new CreaNThread(i); |
37 | + | t[i].join(); |
38 | - | t[i].start(); |
38 | + | } |
39 | - | } |
39 | + | // stampa il valore finale della funzione, il valore atteso ed esce |
40 | pipe.stampavalore(); | |
41 | - | Pipeline x = new Pipeline; //creo un monitor |
41 | + | |
42 | }/*end of main*/ | |
43 | }/*end of class*/ | |
44 | ||
45 | ||
46 | ||
47 | - | // stampa il valore finale della funzione, il valore atteso ed esce |
47 | + | |
48 | - | System.out.println("Il valore finale della funzione " + i.valore() ); |
48 | + | |
49 | int turno; | |
50 | int input[]; | |
51 | int max; | |
52 | ||
53 | public Pipeline(int max){ | |
54 | turno=0; | |
55 | input=new int[max]; | |
56 | - | private int count=0; // privato: no accessi diretti! |
56 | + | this.max=max; |
57 | - | int turno =0; |
57 | + | for(int i=0;i<max;i++)input[i]=i; |
58 | - | int input=0; //sarà il valore di input della funzione successiva, e |
58 | + | } |
59 | - | //corrispondente al valore di output della funzione attuale |
59 | + | |
60 | synchronized int myfunction(int i) { | |
61 | return 2*i+1; | |
62 | - | // il metodo synchronized garantisce mutua esclusione sullo stesso oggetto |
62 | + | } |
63 | - | synchronized static void function() { |
63 | + | |
64 | - | |
64 | + | synchronized void threadBody(int i){ |
65 | - | count+=1; //la mia funzione restituirà dunque il successivo |
65 | + | while(turno!=i) |
66 | - | } |
66 | + | try{ |
67 | wait(); | |
68 | ||
69 | - | syncronized void threadBody(int i){ |
69 | + | }catch(Exception e){} |
70 | - | while(turno!=i)wait(); //se il turno non è quello del thread allora il thread aspetta |
70 | + | |
71 | for(int j=0;j<max;j++) | |
72 | - | //esecuzione |
72 | + | input[j]=myfunction(input[j]); |
73 | - | System.out.println("Thread "+i+" eseguito!"); |
73 | + | System.out.println("Thread "+i+" eseguito!"); |
74 | - | |
74 | + | turno++; |
75 | - | //cambio valore dell'input per il prossimo thread |
75 | + | notifyAll(); |
76 | - | input= function(); |
76 | + | } |
77 | - | |
77 | + | |
78 | - | //turno successivo. Notifico a tutti che ho finito il mio conto. |
78 | + | void stampavalore() { |
79 | - | //essendo dentro ad un while allora di ripartirà e nuovamente entrerà in ciclo solamente |
79 | + | System.out.print("Risultato: "); |
80 | - | //il thread con il permesso |
80 | + | for(int i=0;i<max;i++)System.out.print(input[i]+" "); |
81 | - | notifyAll(); |
81 | + | System.out.println("."); |
82 | - | } |
82 | + | } |
83 | } |