简体   繁体   中英

How can several objects of the same type work with an object of another type at the same time?

I need 4 cashier to serve all clients, but it turns out that each client is processed by each cash desk, which is not correct NOT CORRECT that I have:
Total time 2.5 client #15 cashier #2

Total time 3.0 client #15 cashier #4

Total time 2.4 client #15 cashier #3

Total time 2.0 client #15 cashier #1

Total time 6.0 client #14 cashier #4

Total time 4.0 client #14 cashier #1

Total time 4.8 client #14 cashier #3

CORRECT what I need:

Total time 2.5 client #15 cashier #2

Total time 3.0 client #14 cashier #4

Total time 2.4 client #13 cashier #3

How to implement it correctly?

public class CashierThread extends Thread{
    private TotalClients totalClients;
    private Cashier cashier;
    private double totalTime = 0;

    public CashierThread(TotalClients totalClients, Cashier cashier) {
        this.totalClients = totalClients;
        this.cashier = cashier;
    }

    public synchronized void run() {
        int clients = totalClients.getClients();
        while (clients > 0){
            if(isAlive()) {
                double timeCashier = cashier.getAverageTime();
                totalTime += timeCashier;
                System.out.println("Total time " + totalTime + " client #" + clients + " cashier #" + cashier.getId());
                clients--;
                totalClients.setClients(clients);
                try {
                    this.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class TotalClients {
    private int clients;

    public TotalClients(int clients) {
        this.clients = clients;
    }

    public int getClients() {
        return clients;
    }

    public void setClients(int clients) {
        this.clients = clients;
    }
}


public class Cashier {
    private double averageTime;
    private int id;


    public Cashier(double averageTime, int id) {
        this.averageTime = averageTime;
        this.id = id;
    }

    public double getAverageTime() {
        return averageTime;
    }

    public int getId() {
        return id;
    }
}

There are multiple problems with your run method:

  1. You are maintaining the clients count locally in your loop.
  2. even if you get the client each time from the TotalClients class it'll be too late by the time you call a get after the update, so both the action should be done simultaneously for your specific case.
  3. If your operation fails you can again update the numbers in TotalClients (I'm not covering that scenario in my code).
    public synchronized void run() {
        int clients = totalClients.getClients();
        while (clients > 0){
            if(isAlive()) {
                double timeCashier = cashier.getAverageTime();
                totalTime += timeCashier;
                System.out.println("Total time " + totalTime + " client #" + clients + " cashier #" + cashier.getId());
                try {
                    this.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                clients = totalClients.getClients();
            }
        }
    }

and your TotalClients class should look something like the following:


public class TotalClients {
    private AtomicInteger clients;

    public TotalClients() {
        this.clients = new AtomicInteger(4);
    }


    //get and update clients
    public int getClients() {
        return this.clients.decrementAndGet();
    }


}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM