Server.java


Below is the syntax highlighted version of Server.java from §1.3 Stacks and Queues.


/******************************************************************************
 *  Compilation:  javac Server.java
 *  Execution:    java Server n
 *  Dependencies: Queue.java
 *
 *  Load balancing example.
 *
 ******************************************************************************/

public class Server {
    private Queue<String> list = new Queue<String>();      // list of users
    private int load;                                      // load

    // add a new user to the list
    public void add(String user) {
        list.enqueue(user);
        load++;
    }

    // string representation
    public String toString() {
        // String s = String.format("%5d:  ", load);
        String s = "";
        for (String user : list)
            s += user + " ";
        return s;
    }

    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);

        Server[] servers = new Server[n];
        for (int i = 0; i < n; i++)
            servers[i] = new Server();

        // generate n random jobs and assign to a random processor
        for (int j = 0; j < n; j++) {
            String user = "user" + j;
            int i = StdRandom.uniformInt(n);
            servers[i].add(user);
        }

        // see how even the distribution is by printing out the
        // contents of each server
        for (int i = 0; i < n; i++)
            StdOut.println(i + ": " + servers[i]);
    }
}


Copyright © 2000–2019, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 08:16:52 EDT 2022.