Advertisement
SvOzMaS

Bakery.java

Apr 24th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package bakery;
  2.  
  3. /**
  4.  *
  5.  * @author sergio
  6.  */
  7. public class Bakery extends Thread{
  8.    
  9.     // id del hilo
  10.     public int tid;
  11.    
  12.     // número de hilos a lanzar
  13.     public static final int numThreads = 5;
  14.    
  15.     // contador común sin acceso protegido
  16.     public static int i = 0;
  17.    
  18.     // Array de turnos con acceso protegido
  19.     public static volatile long[] turnos = new long[numThreads];
  20.    
  21.     // Array de persmisos con acceso protegido
  22.     public static volatile boolean[] permisos = new boolean[numThreads];
  23.    
  24.     public Bakery(int id){
  25.         tid = id;
  26.     }
  27.    
  28.    
  29.     @Override
  30.     public void run() {
  31.         for(;;) {
  32.                  
  33.             preProtocolo(tid);  // bloquear turno
  34.            
  35.             //Sección crítica
  36.             System.out.println("Thread_"+ tid + ": Sección crítica. Vuelta " + ++i);
  37.            
  38.             postProtocolo(tid); // desbloquear turno
  39.         }
  40.     }
  41.    
  42.     /*
  43.      * Método de preProtocolo (Bloquea la sección crítica).
  44.      */
  45.     public void preProtocolo(int id) {
  46.        
  47.                 //Pre-protocolo
  48.                 System.out.println("Thread_"+ id + ": Pre-protocolo");
  49.  
  50.                 // El hilo con el índice id pide acceso a la sección crítica
  51.         permisos[id] = true;
  52.  
  53.                 // Encuentra el turno más alto e incrementa en uno el valor del turno del hilo id
  54.         turnos[id] = findMax() + 1;
  55.         permisos[id] = false;
  56.  
  57.        
  58.                 for (int j = 0; j < numThreads; j++){
  59.                         // Si el hilo j es el hilo actual, continua al siguiente hilo
  60.             if (j == id)
  61.                 continue;
  62.            
  63.                         // Espera hasta que sea el turno del hilo j
  64.             while (permisos[j]) {}
  65.  
  66.            
  67.             while (turnos[j] != 0 && (turnos[id] > turnos[j] || (turnos[id] == turnos[j] && id > j))) {}
  68.                          
  69.         }
  70.     }
  71.  
  72.     /*
  73.      * Método de postProtocolo (Desbloquea la sección crítica).
  74.      */
  75.     private void postProtocolo(int id) {
  76.         turnos[id] = 0;
  77.         //Post-protocolo
  78.                 System.out.println("Thread_"+ id + ": Post-protocolo");
  79.     }
  80.  
  81.     /*
  82.      * Método para encontrar el máximo valor en el array de turnos.
  83.      */
  84.     private long findMax() {
  85.        
  86.         long m = turnos[0];
  87.  
  88.         for (int j = 1; j < turnos.length; j++) {
  89.             if (turnos[j] > m)
  90.                 m = turnos[j];
  91.         }
  92.         return m;
  93.     }
  94.        
  95.        
  96.         public static void main(String[] args){
  97.    
  98.         Bakery[] threads = new Bakery[numThreads];
  99.  
  100.         // Inicialización de hilos
  101.         for (int j = 0; j < threads.length; j++) {
  102.             threads[j] = new Bakery(j);
  103.             threads[j].start();
  104.         }
  105.  
  106.     }
  107.    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement