Doubt with Threads and with the Scheduled method

0

I have the following program:

Class Empresa :

public class Empresa {

    List<Treballador> plantilla = new ArrayList();
    public Empresa() {
        plantilla.add(new Treballador("Albert"));
        plantilla.add(new Treballador("Joan"));
        plantilla.add(new Treballador("Maria"));
    }
    public void mostrarTemps() {
        for (Treballador t : plantilla) {
            System.out.println(t.getNom() + " ha treballat " + t.getTemps() + " segons");
        }
    }
}

Class Treballador :

package ioc.dam.m9.uf2.eac1.Ex1;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;

public class Treballador {
    //Nom del treballador
    private String nom;   
    //Ens indica si el treballador està  disponible o no
    private boolean lliure=true;
    //Ens guardarà el "temps" aleatori que li toca treballar
    private int durada=1;
    //Ens ajuda a controlar el "temps" que està  treballant
    private int volta=0;    
    //temps que porta treballat
    private int temps=0;
    //temps que porta treballat
    private long auxTempsIni=0;

    private Calendar cal ;

    public Treballador(String nom) {

        this.nom = nom;   
    }  
    /**
     * Se pone a trabajr. Calculamos la duracion (nombre de vueltas) aleatoras
     */  
    public void treballa() {
        setLliure(false);
        Random r =new Random();
        durada = r.nextInt(20)+5;
    }
    public String getNom() {
        return nom;
    }
    public boolean isLliure() {
        boolean ret = lliure;
        incrementa();
        return ret;
    }    
    public int getTemps() {
        return temps;
    }
    /*MÈTODES PRIVADOS*/
    private void incrementa() {
        volta++;
        setLliure(volta % durada == 0);
    }

    private void setLliure(boolean lliure) {
        if(this.lliure!=lliure){
            this.lliure = lliure;
            mostraNouEstat(lliure);
            if(lliure){
                temps += (System.currentTimeMillis()-auxTempsIni)/1000;
            }else{
                auxTempsIni=System.currentTimeMillis();
            }
        }
    }
    private void mostraNouEstat(boolean nouEstat){
        cal= new GregorianCalendar();
        if (nouEstat) {
            System.out.println(nom + " acaba de treballar a: " + cal.get(Calendar.HOUR_OF_DAY) + ":"
                    + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));
        } else {
            System.out.println(nom + " es posa a treballar a: " + cal.get(Calendar.HOUR_OF_DAY) + ":"
                 + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));         
        }
   }
}

So far so good! But I have to implement 2 classes and I do not know where to start ...

The first is called Encarregat : This every time you are asked to see if there are workers without working (method isLliure ) and if so, put them to work. This class has Empresa as constructor parameter ..

I have this for now:

public class Encarregat {

    Empresa e;

    public Encarregat(Empresa e) {
        this.e = e;
    }
}

But I do not see how to make the calls and the next thing is

Class GestionTrabajadores : What you have to do is put all the workers who are free to work. It is executed every second it starts immediately and waits 60 seconds. Use as many processors as you have. At the end you have to call the showTemps method. The issue is that I have part done but I do not know 100% where I have to call.

I have this for now:

public class GestioTreballadors {
    public static void main(String[] args) throws InterruptedException {
        // ScheduleExecutor lo que hará es:
        // 1- Hara servir tantos hilos como nombre de procesadores disponga 
        final ScheduledExecutorService schExService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
        // EN ESTO TENGO EL PROBLEMA..... //
        final Runnable ob = new GestioTreballadors.new 
Treballador.isLliure();  /// NO SE 100% que poner aquí T_T
        // 2- Lo ejecutara inmediatamente y cada segundo y esperara a finalizar 60 segundos.
        schExService.scheduleWithFixedDelay(ob, 0, 1, TimeUnit.SECONDS);
        schExService.awaitTermination(60, TimeUnit.SECONDS);
        // ShutDown
        schExService.shutdown();
        System.out.println("completado");
        // No se para que usar esto.. :S
        Empresa empresa = new Empresa();
        empresa.mostrarTemps();
    }
}
    
asked by Montse Mkd 09.03.2017 в 19:40
source

0 answers