Visualizzazione dei risultati da 1 a 3 su 3

Discussione: mi potreste spiegare?

  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2007
    Messaggi
    2

    mi potreste spiegare?

    qualcuno mi può spiegare cosa succede in questa classe?grazie
    vi prego è urgente...
    devo scrivere una relazione..e spiegare queste due classi cosa fanno!please


    package it.threads.stations;

    public class FuelMan extends Thread {
    private boolean running = false;
    private boolean isWorking = false;
    FuelStation fuelStation;

    public FuelMan(FuelStation fs) {
    running = true;
    this.fuelStation = fs;
    }

    public boolean isWorking() {
    return isWorking;
    }

    public void run() {
    super.run();
    while(running) {
    try {
    synchronized (this) {
    this.wait();
    //Svegliato
    isWorking = true;
    }
    int wt = (int)(Math.random()*10) + 20; //Wait time
    //System.out.println("WaitTime: "+wt);
    Thread.sleep(wt);
    fuelStation.returnKeysToDriver();
    synchronized (this) {
    isWorking = false;
    }
    } catch (InterruptedException e) {}
    }
    }

    public void stopFuelMan() {
    running = false;
    }
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Oct 2007
    Messaggi
    2
    anche questa..

    package it.threads.stations;

    import it.threads.cars.Car;

    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Hashtable;
    import java.util.Iterator;
    import java.util.TreeSet;

    public class FuelStation extends Thread {
    public static final int BENZ_TYPE = 1;
    public static final int DIESEL_TYPE = 2;
    public static final int BIFUEL_TYPE = 3;

    boolean running = false;
    ArrayList<Car> queue;
    Car activeCar;
    FuelMan fuelMan;
    int type;

    Hashtable<String, Integer> stats;
    int tot;
    float avg;
    int dequeued;

    public FuelStation(int type) {
    this.type = type;
    queue = new ArrayList<Car>();
    fuelMan = new FuelMan(this);
    fuelMan.setName("Fuel Man #"+type);
    fuelMan.start();
    stats = new Hashtable<String, Integer>();
    running = true;
    tot = 0;
    avg = 0;
    dequeued = 0;
    }

    public void enqueueCar(Car c) {
    queue.add(c);
    }

    public void dequeueCar(Car c) {
    queue.remove(c);
    dequeued++;
    }

    public void nextCar() {
    Car c = queue.remove(0);
    //Stats
    String name = c.getName();
    int prev = (stats.get(name) != null)?stats.get(name):0;
    stats.put(name, prev+1);
    tot++;
    avg = ((avg * (tot-1)) + this.getWaitTime(c)) / tot;

    synchronized (c) {
    c.notify();
    }
    }

    private int getWaitTime(Car c) {
    return (int)(Calendar.getInstance().getTimeInMillis() - c.getMills());
    }

    public Car getActiveCar() {
    return activeCar;
    }

    public void setActiveCar(Car activeCar) {
    this.activeCar = activeCar;
    }

    public void giveKeysToFuelMan(Car c) {
    //Set Active Car
    this.setActiveCar(c);
    synchronized (fuelMan) {
    fuelMan.notify();
    }
    }

    public void returnKeysToDriver() {
    Car c = this.getActiveCar();
    synchronized (c) {
    c.notify();
    }

    }

    @Override
    public void run() {
    while (running) {
    //Debug.print("Fuel Station "+this.getName()+" is waiting for next car...");
    if(!fuelMan.isWorking() && !queue.isEmpty()) {
    synchronized (this) {
    this.nextCar();
    }
    }
    try {
    Thread.sleep(10);
    } catch (InterruptedException e) {}
    }
    }

    public void stopFuelStation() {
    running = false;
    fuelMan.stopFuelMan();
    }

    public void printStats() {
    TreeSet<String> tree = new TreeSet<String>(stats.keySet());
    System.out.println("====================== "+this.getName()+" Stats ======================");
    Iterator<String> iter = tree.iterator();
    while (iter.hasNext()) {
    String k = iter.next();
    int v = stats.get(k);
    System.out.println(k+" ha fatto benzina "+v+" volte");
    }
    System.out.println("TOTALE RIFORNIMENTI: "+tot);
    System.out.println("Tempo medio di attesa (in millisecondi): "+avg);
    if(type == BIFUEL_TYPE) {
    System.out.println("Automobili BiFuel passate ad un distributore di Benzina: "+dequeued);
    }
    System.out.println("====================== END "+this.getName()+" Stats ======================");
    }
    }

  3. #3
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328

    Moderazione

    LEggere il regolamento please... postare il codice all'interno degli appositi TAG, urgente non esiste in nessun forum, i titoli non si mettono come il formaggio sulla pasta e non si viene su un forum per farsi fare le relazioni, ma per discutere di problemi di programmazione (altrimenti il forum si sarebbe chiamato in modo diverso).

    Chiudo.

    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.