SHARE
TWEET

Untitled

a guest Feb 21st, 2020 116 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ejemplos;
  2.  
  3. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
  4. import es.urjc.etsii.code.concurrency.SimpleSemaphore;
  5.  
  6. public class Ejer16 {
  7.  
  8.     private static final int N_HILOS = 4;
  9.  
  10.     public static int posicion = 0;
  11.     public static int pintadoLetra = 0;
  12.     private volatile static SimpleSemaphore barrera = new SimpleSemaphore(0);
  13.  
  14.     public static void hilo() {
  15.         enterMutex("posicion");
  16.         String letra = obtenerLetra(posicion++);
  17.         exitMutex("posicion");
  18.  
  19.         while (true) {
  20.             print(letra);
  21.  
  22.             enterMutex();
  23.             pintadoLetra++;
  24.             if (pintadoLetra < N_HILOS) {
  25.                 exitMutex();
  26.                 barrera.acquire();
  27.             } else {
  28.                 pintadoLetra = 0;
  29.                 exitMutex();
  30.                 println("-");
  31.                 barrera.release(N_HILOS - 1);
  32.             }
  33.         }
  34.     }
  35.  
  36.     public static String obtenerLetra(int i) {
  37.         if (i == 0) {
  38.             return "A";
  39.         } else if (i == 1) {
  40.             return "B";
  41.         } else if (i == 2) {
  42.             return "C";
  43.         } else if (i == 3) {
  44.             return "D";
  45.         }
  46.         return null;
  47.     }
  48.  
  49.     public static void main(String[] args) {
  50.         createThreads(N_HILOS, "hilo");
  51.         startThreadsAndWait();
  52.     }
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top