Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. package Semáforos;
  2.  
  3. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
  4.  
  5. import es.urjc.etsii.code.concurrency.SimpleSemaphore;
  6.  
  7. public class Starvation {
  8.    
  9.     public static SimpleSemaphore sem;
  10.     public static volatile boolean listo = false;
  11.    
  12.     public static void restaurante() {
  13.         while(true) {
  14.             printlnI("Preparando comida");
  15.             if(!listo) {
  16.             sem.acquire();
  17.             listo = true;
  18.             sem.release();
  19.             }
  20.             else printlnI("Esperando al repartidor...");
  21.         }
  22.     }
  23.    
  24.     public static void repartidorGlovo() {
  25.         while(true) {
  26.             if (listo) {
  27.                 printlnI("No me pagan lo suficiente");
  28.                 sleep(500);
  29.                 sem.acquire();
  30.                 listo = false;
  31.                 sem.release();
  32.             }
  33.             else printlnI("Esperando a la comida");
  34.         }
  35.     }
  36.    
  37.    
  38.    
  39.    
  40.     public static void main(String[] args) {
  41.         sem = new SimpleSemaphore(1);
  42.         createThread("restaurante");
  43.         createThread("repartidorGlovo");
  44.         startThreadsAndWait();
  45.        
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement