I want a thread to execute in the background every 500 milliseconds. To do that, I extended a Thread, implemented ActionListener and put the class that I extended into a Timer. The Timer calls run() every 500 milliseconds. However, my whole Swing GUI freezes up when this thread is downloading stuff from the Internet. I want it to run in the background, without freezing up the GUI while it waits for IO to finish. I also the downloader to finish downloading before we wait 500 milliseconds.
gogogo() is called to initialize the whole process:
public final class Downloader extends Thread implements ActionListener { public static void gogogo() { t= new Downloader(); new Timer(500, (ActionListener) t).start(); } public void run() { doStuff(); //the code that i want repeatedly called } public void actionPerformed(ActionEvent e) { run(); } }
Just start the thread once, make it loop, and do
Thread.sleep(500L)with each iteration. That probably makes more sense that starting a brand new thread every 500ms. No reason to incur the associated cost if you can avoid it.