Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. package ejercicios;
  2.  
  3. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
  4.  
  5. public class Ejercicio09 {
  6.  
  7.     private static final int N_FRAGMENTOS = 10;
  8.     private static final int N_HILOS = 3;
  9.  
  10.     private static volatile int[] fichero = new int[N_FRAGMENTOS];
  11.  
  12.     // Add the attributes you need
  13.     private static volatile int siguienteFragmento = 0;
  14.  
  15.     private static int descargaDatos(int numFragmento) {
  16.         sleepRandom(1000);
  17.         return numFragmento * 2;
  18.     }
  19.  
  20.     private static void mostrarFichero() {
  21.         println("--------------------------------------------------");
  22.         print("File = [");
  23.         for (int i = 0; i < N_FRAGMENTOS; i++) {
  24.             print(fichero[i] + ",");
  25.         }
  26.         println("]");
  27.     }
  28.  
  29.     public static void downloader() {
  30.  
  31.         while (true) {
  32.             enterMutex();
  33.             if (siguienteFragmento == N_FRAGMENTOS) {
  34.                 exitMutex();
  35.                 break;
  36.             }
  37.             int frag = siguienteFragmento;
  38.             siguienteFragmento++;
  39.             exitMutex();
  40.            
  41.             fichero[frag] = descargaDatos(frag);
  42.         }
  43.     }
  44.  
  45.     public static void main(String[] args) {
  46.  
  47.         createThreads(N_HILOS, "downloader");
  48.  
  49.         startThreadsAndWait();
  50.  
  51.         mostrarFichero();
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement