Advertisement
Jerkiller

javaccc

Jun 9th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 MAX=4;
  14.         final int index;
  15.         Pipeline p;
  16.  
  17.         CreaNThread(int i,Pipeline pipe) {
  18.                 index = i; p=pipe;
  19.         }
  20.  
  21.         public void run(){
  22.                 p.threadBody(index);
  23.         }
  24.        
  25.         public static void main(String args[]) throws InterruptedException {
  26.                 int i;
  27.                 Pipeline pipe= new Pipeline(MAX);
  28.                 Thread t[] = new Thread[NTHREAD];
  29.                 // crea 10 thread e li esegue
  30.                 for(i=0;i<NTHREAD;i++) {
  31.                         t[i] = new CreaNThread(i,pipe);
  32.                         t[i].start();
  33.                 }
  34.  
  35.         // attende la terminazione dei thread
  36.         for(i=0;i<NTHREAD;i++) {
  37.             t[i].join();
  38.         }
  39.                 // stampa il valore finale della funzione, il valore atteso ed esce
  40.                 pipe.stampavalore();
  41.  
  42.         }/*end of main*/
  43. }/*end of class*/
  44.  
  45.  
  46.  
  47.  
  48. class Pipeline {
  49.         int turno;
  50.         int input[];
  51.         int max;
  52.        
  53.         public Pipeline(int max){
  54.                 turno=0;
  55.                 input=new int[max];
  56.                 this.max=max;
  57.                 for(int i=0;i<max;i++)input[i]=i;
  58.         }
  59.  
  60.         synchronized int myfunction(int i) {
  61.                 return 2*i+1;
  62.         }
  63.  
  64.         synchronized void threadBody(int i){
  65.                 while(turno!=i)
  66.                 try{
  67.                     wait();
  68.                    
  69.                 }catch(Exception e){}
  70.                
  71.                 for(int j=0;j<max;j++)
  72.                         input[j]=myfunction(input[j]);
  73.                 System.out.println("Thread "+i+" eseguito!");
  74.                 turno++;
  75.                 notifyAll();
  76.         }
  77.  
  78.         void stampavalore() {
  79.                 System.out.print("Risultato: ");
  80.                 for(int i=0;i<max;i++)System.out.print(input[i]+" ");
  81.                 System.out.println(".");
  82.         }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement