Our client has an imap mail account of 400 folders. We fetch mails using java mail api. On the start of the program we need all the unread message count of each folder.
Suppose we have the foldernames in a string array
String arryFolderNames[] = { "f1", "f2"..
And for each folder:
for (String folderName : arryFolderNames) {
IMAPFolder imapFolder = MailStore.getFolder(folderName);
//takes 100-300 milliseconds
int unreadCount = imapFolder.getUnreadMessageCount();
}
To get the unread message count of a single folder takes at least 100 milliseconds and totally the whole process takes more than 40 seconds, but the customer demands only 5-6 sec. at most. I have to optimize this process:
All folders are closed and I perform getUnreadMessgeCount on a closed folder. But when I check before call of getUnreadCount whether folder is opened and open the folder, getUnreadCount and close again, then folder open/close process takes additional time and I do not get any performance gain.
When I open all 400 folder at once, and keep them open, everything becomes slower and as think it is not a good practice to have all folders opened always.
When I put the folder.getUnreadCount in different Threads, (Thread blocks with 10 size, or 1 thread for a folder, with 400 threads, or 4 threads with 100 folders, etc.) there is no gain and processing all folders takes longer time.
Any suggestions, ideas?