Ver Mensaje Individual
  #9 (permalink)  
Antiguo 05/05/2013, 05:00
Avatar de rgf1987
rgf1987
 
Fecha de Ingreso: diciembre-2012
Ubicación: Asturias
Mensajes: 269
Antigüedad: 11 años, 3 meses
Puntos: 22
Respuesta: Ejemplo de Threat en una array, es correcto ?

Creandolo todo desde el main seria algo parecido a lo siguiente:

Main:
Código JAVA:
Ver original
  1. public class Main {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.         Oferta oferta = new Oferta();
  8.         oferta.start();
  9.         Subscriptor sub1 = new Subscriptor("Uno", oferta);
  10.         Subscriptor sub2 = new Subscriptor("Dos", oferta);
  11.         Subscriptor sub3 = new Subscriptor("Tres", oferta);
  12.         Subscriptor sub4 = new Subscriptor("Cuatro", oferta);
  13.         Subscriptor sub5 = new Subscriptor("Cinco", oferta);
  14.         Subscriptor sub6 = new Subscriptor("Seis", oferta);
  15.         Subscriptor sub7 = new Subscriptor("Siete", oferta);
  16.        
  17.         List<Subscriptor> listaSubs = new ArrayList<Subscriptor>();
  18.         listaSubs.add(sub1);
  19.         listaSubs.add(sub2);
  20.         listaSubs.add(sub3);
  21.         listaSubs.add(sub4);
  22.         listaSubs.add(sub5);
  23.         listaSubs.add(sub6);
  24.         listaSubs.add(sub7);
  25.        
  26.         for(int i=0;i<listaSubs.size();i++){
  27.             listaSubs.get(i).start();
  28.         }  
  29.  
  30.     }
  31.  
  32. }

Clase Subscriptor:
Código JAVA:
Ver original
  1. public class Subscriptor extends Thread {
  2.  
  3.     private String nombre;
  4.     private Oferta oferta;
  5.  
  6.     public String getNombre() {
  7.         return this.nombre;
  8.     }
  9.     public Subscriptor(String nombre, Oferta oferta){
  10.         this.nombre=nombre;
  11.         this.oferta=oferta;
  12.     }
  13.     public void run(){
  14.         int num = (int)(Math.random()*10);
  15.         try {
  16.             Thread.sleep(num*1000);
  17.             oferta.apuntarse(this);
  18.         } catch (InterruptedException e) {
  19.             // TODO Auto-generated catch block
  20.             e.printStackTrace();
  21.         }
  22.        
  23.     }
  24.  
  25. }

Clase Oferta:

Código JAVA:
Ver original
  1. public class Oferta extends Thread{
  2.     List<Subscriptor> listaSubscriptores = new ArrayList<Subscriptor>();
  3.     public void apuntarse(Subscriptor subs){
  4.         if(listaSubscriptores.size()<5){
  5.             this.listaSubscriptores.add(subs);
  6.             System.out.println("El subscriptor "+subs.getNombre()+" se ha apuntado a la oferta");
  7.             }
  8.     }
  9.    
  10.  
  11. }

Si hay que crear los subscriptores en el main y luego iniciarlos desde un solo hilo la cosa se complica bastante, ya que estarias trabajando con el objeto Subscriptor recursivamente y habria que pasarle el objeto oferta a cada subscriptor de la siguiente forma:

Clase main:
Código JAVA:
Ver original
  1. public class Main {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.         Oferta oferta = new Oferta();
  8.         oferta.start();
  9.        
  10.         Subscriptor sub1 = new Subscriptor("Uno");
  11.         Subscriptor sub2 = new Subscriptor("Dos");
  12.         Subscriptor sub3 = new Subscriptor("Tres");
  13.         Subscriptor sub4 = new Subscriptor("Cuatro");
  14.         Subscriptor sub5 = new Subscriptor("Cinco");
  15.         Subscriptor sub6 = new Subscriptor("Seis");
  16.         Subscriptor sub7 = new Subscriptor("Siete");
  17.        
  18.         List<Subscriptor> listaSubs = new ArrayList<Subscriptor>();
  19.         listaSubs.add(sub1);
  20.         listaSubs.add(sub2);
  21.         listaSubs.add(sub3);
  22.         listaSubs.add(sub4);
  23.         listaSubs.add(sub5);
  24.         listaSubs.add(sub6);
  25.         listaSubs.add(sub7);
  26.        
  27.         Subscriptor inicial = new Subscriptor("Inicial", oferta, listaSubs);
  28.         inicial.run();
  29.        
  30.  
  31.     }
  32.  
  33. }

Clase subscriptor:
Código JAVA:
Ver original
  1. public class Subscriptor extends Thread {
  2.  
  3.     private String nombre;
  4.     private Oferta oferta;
  5.     List<Subscriptor> listaSubs;
  6.  
  7.    
  8.     public Oferta getOferta() {
  9.         return oferta;
  10.     }
  11.     public void setOferta(Oferta oferta) {
  12.         this.oferta = oferta;
  13.     }
  14.     public void setNombre(String nombre) {
  15.         this.nombre = nombre;
  16.     }
  17.     public String getNombre() {
  18.         return this.nombre;
  19.     }
  20.    
  21.     public Subscriptor(String nombre){
  22.         this.nombre=nombre;
  23.     }  
  24.     public Subscriptor(String nombre, Oferta oferta,
  25.             List<Subscriptor> listaSubs) {
  26.         this.oferta=oferta;
  27.         this.nombre=nombre;    
  28.         this.listaSubs=listaSubs;
  29.     }
  30.     public void run(){
  31.         if(listaSubs!=null){               
  32.             for(int i=0;i<this.listaSubs.size();i++){
  33.                 Subscriptor s=listaSubs.get(i);
  34.                 s.setOferta(this.oferta);
  35.                 s.start();
  36.             }
  37.         }
  38.         else{
  39.             int num = (int)(Math.random()*10);
  40.             try {
  41.                 Thread.sleep(num*1000);
  42.                 oferta.apuntarse(this);
  43.             } catch (InterruptedException e) {
  44.                 // TODO Auto-generated catch block
  45.                 e.printStackTrace();
  46.             }
  47.         }  
  48.     }
  49. }

Creo que las dos versiones estan implementadas correctamente, sino es asi, que me lo corrijan que hay gente por aqui que sabe bastante mas que yo eje.

Un saludo y espero que te haya servido.