Creating standalone program to monitor the mailbox
I am trying to create a standalone application which needs to monitor my mailbox.
I have created 2 classes.
MailScanner.class = which is the parent program. On execution of this program it creates objects of InboxMonitor class. Each InboxMonitor object is associated with a mail account. If any new mail arrives in the specified mail account then the event listener automatically listens to it.
To keep this parent program running i have coded an infinite while loop which sleeps for every 5 secs. Then the program resumes. Hence the event listener will be notified only after 5 secs.(Probably need to find a much better way to do it)
InboxMonitor.class = This is the event listener class for recording new incoming messages. On arrival of a new mail the appropriate action is taken.
Now i need to keep my MailScanner.class continuously running.
Hence i have coded an infinite loop in my MailScanner.class.
-- Here goes the code for creating the instances of InboxMonitor classes and register the listener to the inbox
while (true)
{
Thread.sleep(5000);
}
This piece of code keeps the mailscanner class alive but sleeps for 5 secs. Hence the incoming mails will get pooled for every 5 sec.
But what i require is a program that continuously runs without any sleep and listen to any incoming request without any delay.
If i keep the the code as while(true){}, then the cpu usage is at maximum.
Is there any alternate way to keep my MailScanner class active and alive ?
I am also a novice in thread programming. So can anybody help me out
Thanks in advance