Skip to content

Commit

Permalink
Load each list on it's own thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhagander committed Jun 20, 2010
1 parent 19ec479 commit 386dbae
Showing 1 changed file with 67 additions and 45 deletions.
112 changes: 67 additions & 45 deletions src/net/hagander/mailinglistmoderator/MailinglistModerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,63 +144,85 @@ private void populateServers() {

Runnable r = new Runnable() {
public void run() {
Vector<Thread> threads = new Vector<Thread>();

for (int i = 0; i < servers.size(); i++) {
ListServer s = servers.get(i);
try {
s.Populate();
} catch (Exception e) {
final String msg = String.format("%s", e);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), msg,
Toast.LENGTH_LONG).show();
final ListServer s = servers.get(i);
Thread t = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {
s.Populate();
} catch (Exception e) {
final String msg = String.format("%s", e);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), msg,
Toast.LENGTH_LONG).show();
}
});
}
});
continue;
}

/*
* Since servers are sorted by number of messages, re-sort
* the list when it has updated.
*
* We run this once in each loop so that servers with
* messages to moderate on will "bubble up" to the top as we
* run.
*/
Collections.sort(servers, new Comparator<ListServer>() {
public int compare(ListServer server1,
ListServer server2) {
if (!server1.isPopulated()) {
if (!server2.isPopulated()) {
// Neither server is populated, sort by name
return server1.getName().compareTo(server2.getName());
}
// server1 is not populated, server2 is ==> server2 is bigger
return 1;
}
else if (!server2.isPopulated()) {
// server2 is not populated, server1 is ==> server1 is bigger.
return -1;
}
// Both servers are populated
if (server2.count() == server1.count()) {
// Count is identical, so compare by name
return server1.getName().compareTo(server2.getName());
}
// Both servers have valid values, and they are not the same,
// so return the comparison of them.
return server2.count() - server1.count();
}
/*
* Since servers are sorted by number of messages, re-sort
* the list when it has updated.
*
* We run this once in each loop so that servers with
* messages to moderate on will "bubble up" to the top as we
* run.
*/
sortServers();
}
});
threads.add(t);
t.start();
}

notifyServersChanged();
/*
* Now wait until all threads are done, but join()ing on them
*/
for (int i = 0; i < threads.size(); i++) {
try {
threads.get(i).join();
} catch (InterruptedException e) {
}
}
}
};
Thread t = new Thread(r, "ServerPopulatingThread");
t.start();
}

private void sortServers() {
Collections.sort(servers, new Comparator<ListServer>() {
public int compare(ListServer server1,
ListServer server2) {
if (!server1.isPopulated()) {
if (!server2.isPopulated()) {
// Neither server is populated, sort by name
return server1.getName().compareTo(server2.getName());
}
// server1 is not populated, server2 is ==> server2 is bigger
return 1;
}
else if (!server2.isPopulated()) {
// server2 is not populated, server1 is ==> server1 is bigger.
return -1;
}
// Both servers are populated
if (server2.count() == server1.count()) {
// Count is identical, so compare by name
return server1.getName().compareTo(server2.getName());
}
// Both servers have valid values, and they are not the same,
// so return the comparison of them.
return server2.count() - server1.count();
}
});

notifyServersChanged();
}

/**
* Create the menu for when the Menu button is pressed.
*/
Expand Down

0 comments on commit 386dbae

Please sign in to comment.