001package algs13;
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac Server.java
005 *  Execution:    java Server N
006 *  Dependencies: Queue.java
007 *
008 *  Load balancing example.
009 *
010 *************************************************************************/
011
012public class XServer {
013        private final Queue<String> list = new Queue<>();      // list of users
014        private int load;                                      // load
015
016        // add a new user to the list
017        public void add(String user) {
018                list.enqueue(user);
019                load++;
020        }
021
022        // string representation
023        public String toString() {
024                // String s = String.format("%5d:  ", load);
025                String s = "";
026                for (String user : list)
027                        s += user + " ";
028                return s;
029        }
030
031        public static void main(String[] args) {
032                int N = Integer.parseInt(args[0]);
033
034                XServer[] servers = new XServer[N];
035                for (int i = 0; i < N; i++)
036                        servers[i] = new XServer();
037
038                // generate N random jobs and assign to a random processor
039                for (int j = 0; j < N; j++) {
040                        String user = "user" + j;
041                        int i = StdRandom.uniform(N);
042                        servers[i].add(user);
043                }
044
045                // see how even the distribution is by printing out the
046                // contents of each server
047                for (int i = 0; i < N; i++)
048                        StdOut.println(i + ": " + servers[i]);
049        }
050}