Advertisement
Guest User

Maquinas Robots

a guest
Jun 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.11 KB | None | 0 0
  1. package CadenaMontaje;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.concurrent.BlockingQueue;
  5. import java.util.concurrent.LinkedBlockingQueue;
  6.  
  7. public class AlmacenPiezas {
  8.    
  9.     public static int MAX_PIEZAS = 5;
  10.     private List<BlockingQueue<Double>> colas; //Lista de blockingueues
  11.    
  12.     public AlmacenPiezas(int numTiposPieza){
  13.         List<BlockingQueue<Double>> colasAux = new ArrayList<BlockingQueue<Double>> ();
  14.         for(int i =0; i<numTiposPieza; i++){
  15.            colasAux.add(new LinkedBlockingQueue<Double>(MAX_PIEZAS));
  16.         }
  17.         colas = new ArrayList<BlockingQueue<Double>> (colasAux);
  18.     }
  19.    
  20.     public void almacenarPieza(int tipoPieza, double pieza) throws InterruptedException{
  21.         colas.get(tipoPieza).put(pieza);
  22.     }
  23.     public double recogerPieza (int tipoPieza) throws InterruptedException{
  24.         return colas.get(tipoPieza).take();
  25.     }
  26. }
  27.  
  28.  
  29.  
  30. //--------------------------------------------------------------------------------------//
  31.  
  32.  
  33. package CadenaMontaje;
  34.  
  35. import java.util.logging.Level;
  36. import java.util.logging.Logger;
  37.  
  38. public class CadenaMontaje {
  39.     private static int NUM_TIPOS_PIEZAS = 3;
  40.     private static int NUM_ROBOTS = 3;
  41.     private AlmacenPiezas almacen = new AlmacenPiezas(NUM_TIPOS_PIEZAS);
  42.        
  43.     private void montarPieza(int tipoPieza, double pieza) throws InterruptedException{
  44.         Thread.sleep((long) Math.random()*1000);
  45.         System.out.println(Thread.currentThread().getName()+ ": " + pieza);
  46.     }    
  47.    
  48.     public void robot(int numRobot){
  49.         try {
  50.             while(true){
  51.                 for(int i =0 ; i<NUM_TIPOS_PIEZAS; i++){
  52.                     double pieza = almacen.recogerPieza(i);
  53.                     montarPieza(i, pieza);
  54.                 }                
  55.             }
  56.            
  57.         }catch (InterruptedException ex) {
  58.                 Logger.getLogger(CadenaMontaje.class.getName()).log(Level.SEVERE, null, ex);
  59.         }  
  60.     }  
  61.    
  62.     private double fabricarPieza(int tipoPieza) throws InterruptedException{
  63.         Thread.sleep( (long)Math.random()*1000);
  64.         double pieza = tipoPieza+Math.random();
  65.         System.out.println(Thread.currentThread().getName()+ ": "+pieza);
  66.         return pieza;
  67.     }    
  68.     public void maquina(int numMaquina, int tipoPieza) {
  69.          try {
  70.             while(true){
  71.            
  72.                 double pieza = fabricarPieza(tipoPieza);
  73.                 almacen.almacenarPieza(tipoPieza, pieza);
  74.             }
  75.            
  76.         }catch (InterruptedException ex) {
  77.                 Logger.getLogger(CadenaMontaje.class.getName()).log(Level.SEVERE, null, ex);
  78.         }        
  79.     }
  80.    
  81.     public void exec() {
  82.  
  83.         for (int i = 0; i < NUM_TIPOS_PIEZAS; i++) {
  84.             int numMaquina = i;
  85.             new Thread(() -> maquina(numMaquina, numMaquina), "Maquina-" + numMaquina).start();
  86.         }
  87.  
  88.         for (int i = 0; i < NUM_ROBOTS; i++) {
  89.             int numRobot = i;
  90.             new Thread(() -> robot(numRobot), "Robot-" + numRobot).start();
  91.         }
  92.     }
  93.    
  94.     public static void main(String[] args) {
  95.         new CadenaMontaje().exec();
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement