Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. package Ejercicios;
  2.  
  3. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.createThread;
  4. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.sleepRandom;
  5. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.startThreadsAndWait;
  6.  
  7. public class Cliente_servidor {
  8.  
  9.     public static volatile int numero;
  10.     public static volatile boolean peticionHecha, peticionCompletada;
  11.    
  12.    
  13.     public static void cliente() {
  14.        
  15.         while(true) {
  16.             numero = (int)(Math.random()*9);   
  17.             peticionCompletada = false;
  18.             peticionHecha = true;      
  19.             while(!peticionCompletada);    
  20.             System.out.println("Dato: " + numero);
  21.         }
  22.            
  23.     }
  24.  
  25.     public static void servidor() {
  26.         while(true) {
  27.             while(!peticionHecha);
  28.             numero += 7;
  29.             peticionHecha=false;
  30.             peticionCompletada=true;
  31.         }
  32.            
  33.        
  34.     }
  35.  
  36.     public static void main(String[] args) {
  37.         peticionHecha = peticionCompletada = false;
  38.         createThread("cliente");
  39.         createThread("servidor");
  40.         startThreadsAndWait();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement