Java Reference
In-Depth Information
public static void main(String[] args) throws Exception {
new DBTestRunner();
}
DBTestRunner() throws Exception {
clients = new DBTester[numberOfClients];
startClients();
waitForClientsToDie();
displayStatistics();
}
private void startClients() throws Exception {
for (int i = 0; i < numberOfClients; i++) {
String clientName = "Client " + i;
clients[i] = new DBTester(clientName, rentalsPerClient, dvdUpc);
clients[i].start();
}
}
private void waitForClientsToDie() throws Exception {
// wait for them all to finish
for (DBTester client : clients) {
client.join();
}
}
It is important to realize that even though the client threads are all in
TERMINATED
state by
the time the statistics are being generated, the
DBTester
objects still exist, and we can still call
the public methods on that object to get the information needed for our statistics.
private void displayStatistics() {
// display some statistics
System.out.println();
formatLine("========", "========", "========", "========", "========");
formatLine("Client #", "Rented", "No stock", "Timeout", "Total");
formatLine("--------", "--------", "--------", "--------", "--------");
for (DBTester client : clients) {
formatLine(client.getName(),
client.getSuccessfulRentals(),
client.getOutOfStock(),
client.getTimeouts());
}
formatLine("========", "========", "========", "========", "========");
}