Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. package ejercicios;
  2.  
  3. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
  4. import static es.urjc.etsii.code.concurrency.SimpleSemaphore.*;
  5.  
  6. import es.urjc.etsii.code.concurrency.SimpleSemaphore;
  7.  
  8. public class ejercicio04bSemaforos {
  9.  
  10.     static final int N_CLIENTES = 3;
  11.     static final int N_SERVIDORES = 1;
  12.    
  13.     static final int TYPE_CLIENTE = 0;
  14.     static final int TYPE_SERVIDOR = 1;
  15.    
  16.     static final boolean DEBUG = true;
  17.    
  18.     static volatile int idCliente = 0;
  19.     static volatile int idServidor = 0;
  20.     static volatile int peticion = 0;
  21.    
  22.     private static SimpleSemaphore servidorDisponible = new SimpleSemaphore(0);
  23.     private static SimpleSemaphore peticionProcesada = new SimpleSemaphore(0);
  24.     private static SimpleSemaphore peticionEnviada = new SimpleSemaphore(0);
  25.    
  26.     public static void printI(String string, int id, int type) {
  27.         if (DEBUG) {
  28.             //String mensaje;
  29.             if (type == TYPE_SERVIDOR) {
  30.                 printlnI("[SERVIDOR - "+ id +"]"+ string);
  31.             }else if (type == TYPE_CLIENTE) {
  32.                 printlnI("[CLIENTE - "+ id +"]"+ string);
  33.             }
  34.         }
  35.     }
  36.  
  37.     public static void cliente() {
  38.         int id;
  39.         enterMutex("idCliente");
  40.         id = idCliente++;
  41.         exitMutex("idCliente");
  42.         printI("online", id, TYPE_CLIENTE);
  43.        
  44.         while (true) {
  45.             int peticionLocal = (id * 10) + (int)(Math.random() * 9);
  46.             servidorDisponible.acquire();
  47.             printI("Enviando peticion "+peticionLocal, id, TYPE_CLIENTE);
  48.             peticion = peticionLocal;
  49.             peticionEnviada.release();
  50.             sleepRandom(1000);
  51.            
  52.             peticionProcesada.acquire();
  53.             servidorDisponible.release();
  54.             printI("Peticion procesada "+peticion, id, TYPE_CLIENTE);
  55.            
  56.         }
  57.     }
  58.  
  59.     public static void servidor() {
  60.         int id;
  61.         enterMutex("idServidor");
  62.         id = idServidor++;
  63.         exitMutex("idServidor");
  64.         printI("online", id, TYPE_SERVIDOR);
  65.         servidorDisponible.release();
  66.        
  67.         while (true) {
  68.             peticionEnviada.acquire();
  69.             peticion++;
  70.             printI("Peticion procesada "+peticion, id, TYPE_SERVIDOR);
  71.             peticionProcesada.release();
  72.             //servidorDisponible.release();
  73.         }
  74.     }
  75.  
  76.     public static void main(String[] args) {
  77.        
  78.         createThreads(N_CLIENTES, "cliente");
  79.         createThreads(N_SERVIDORES, "servidor");
  80.         startThreadsAndWait();
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement