java - Background job running without affecting the rest of the gui -


i'm asking assistance concerning general approach.

i have written java code check mailbox unread mails on buttonclick.

now want code permanently run in background , check mailbox every 2 minutes.

bad idea:

while(true) { checkmails(); thread.sleep(120000); } 

the rest of graphical interface freezes, there has happen magic threads, suppose.

how realized?

either use javax.swing.timer, or swingworker, although latter bit overkill in scenario.


if task not modify swing components, why not spawn separate thread, such

thread t = new thread(new runnable(){     @override     public void run(){         while(!thread.currentthread().isinterrupted()){             //do stuff              try{                 thread.sleep(120000);             }catch(interruptedexception e){                 thread.currentthread().interrupt();             }         }     } }); t.start(); 

it's important note you'll want use boolean ensure thread created once, since there's potential new thread spawn each button click, not want.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -