此代码有问题,我不知道为什么它没有完成指定的任务:将负载平衡显示在表中而不是图形中。该代码没有错误,因此,解决该问题的任何建议将对您有很大帮助。这是代码:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class LoadBalance extends JFrame{
private static final long serialVersionUID = 1L;
JTable jt;
public LoadBalance(RandomQueue<Queue<Integer>> servers, double[] lengths) {
jt = new JTable();
jt.setBounds(50,50,200,230);
JScrollPane js = new JScrollPane(jt);
this.add(js);
this.setSize(300,400);
this.setVisible(true);
}
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
int s = Integer.parseInt(args[2]);
// Create server queues.
RandomQueue<Queue<Integer>> servers = new RandomQueue<Queue<Integer>>();
for (int i = 0; i < m; i++) {
servers.enqueue(new Queue<Integer>());
}
// Assign an item to a server
for (int j = 0; j < n; j++) {
// Pick a random server, update if new min.
Queue<Integer> min = servers.sample();
for (int k = 1; k < s; k++) {
Queue<Integer> queue = servers.sample();
if (queue.length() < min.length()) min = queue;
}
// min is the shortest server queue
min.enqueue(j);
}
int i = 0;
double[] lengths = new double[m];
for (Queue<Integer> queue : servers) {
lengths[i++] = queue.length();
}
new LoadBalance(servers, lengths);
}
}