Advertisement
nipsn

p1pc-post2

Oct 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package ejercicios;
  2.  
  3. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
  4.  
  5. import java.util.Arrays;
  6.  
  7. import es.urjc.etsii.code.concurrency.SimpleSemaphore;
  8.  
  9. public class practica1 {
  10.     public static final int N_PROCESOS = 5;
  11.     public static final int N_DATOS = 16;
  12.     public static int nPares = 8;
  13.  
  14.     public static int procesosEnEspera = 0;
  15.    
  16.     private static SimpleSemaphore mutex1;
  17.     private static SimpleSemaphore mutex2;
  18.     private static SimpleSemaphore mutex3;
  19.     private static SimpleSemaphore semaforo1;
  20.    
  21.     private static int[] datos = new int[N_DATOS];
  22.     private static int[] resultados = new int[nPares];
  23.    
  24.     private volatile static int totalSumados = 0;
  25.     private volatile static int posResultados = 0;
  26.     private volatile static boolean terminado = false;
  27.    
  28.     private static int suma(int a, int b) {
  29.         sleepRandom(1000);
  30.         return a + b;
  31.     }
  32.    
  33.     private static void inicializaDatos() {
  34.         for (int i = 0; i < N_DATOS; i++) {
  35.             datos[i] = (int) (Math.random() * 11);
  36.         }
  37.         for(int i = 0;i < nPares;i++) {
  38.             resultados[i] = -1;
  39.         }
  40.         println("Los datos a sumar son: " + Arrays.toString(datos));
  41.     }
  42.    
  43.     private static boolean estanSumados(boolean[] a) {
  44.         for (boolean value : a) {
  45.             if (!value)
  46.                 return false;
  47.         }
  48.         return true;
  49.        
  50.     }
  51.    
  52.     private static void sumador() {
  53.         while(!terminado) {
  54.  
  55.             mutex1.acquire();
  56.             int a = datos[totalSumados*2];
  57.             int b = datos[totalSumados*2 + 1];
  58.             totalSumados++;
  59.             mutex1.release();
  60.  
  61.             int res = suma(a,b);
  62.            
  63.             mutex2.acquire();
  64.             resultados[posResultados] = res;
  65.             posResultados++;
  66.             mutex2.release();
  67.  
  68.             if(totalSumados == nPares-1) {
  69.                
  70.                 mutex3.acquire();
  71.                 procesosEnEspera++;
  72.                 if(procesosEnEspera < N_PROCESOS) {
  73.                     mutex3.release();
  74.                     semaforo1.acquire();
  75.                 } else {
  76.                     mutex3.release();
  77.                    
  78.                     for(int i = 0;i < nPares * 2;i++) {
  79.                         if(i < nPares) {
  80.                             datos[i] = resultados[i];
  81.                         } else {
  82.                             datos[i] = -1;
  83.                         }
  84.                     } // copia un array a otro y lo limpia
  85.                    
  86.                     nPares/=2;
  87.                     totalSumados = 0;
  88.                     posResultados = 0;
  89.                     for(int i = 0;i < N_PROCESOS;i++) {
  90.                         semaforo1.release();
  91.                     }
  92.                 }
  93.             }
  94.            
  95.            
  96.            
  97.         }
  98.        
  99.     }
  100.    
  101.     public static void main(String[] args) {
  102.        
  103.         mutex1 = new SimpleSemaphore(1);
  104.         mutex2 = new SimpleSemaphore(1);
  105.         mutex3 = new SimpleSemaphore(1);
  106.         semaforo1 = new SimpleSemaphore(0);
  107.  
  108.  
  109.     }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement