File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes Problems with Thread.sleep() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Problems with Thread.sleep()" Watch "Problems with Thread.sleep()" New topic

Problems with Thread.sleep()

Brett Fitzgerald

Joined: Mar 21, 2006
Posts: 6
I'm trying to write an applet that gets information from a file every 30 seconds and displays it to a series of labels. My code looks like this:

public class App extends Applet{
Label lblTimeStamp;
WeatherData data;
String path = "update.dat";

public void init(){
resize(300, 800);
setLayout(new FlowLayout());
lblTimeStamp = new Label("lblTimeStamp");
lblTimeStamp.setFont(new Font("Serif", Font.BOLD, 12));


FileInputStream fs = new FileInputStream(path);
ObjectInputStream is = new ObjectInputStream(fs);
data = (WeatherData)is.readObject();
lblTimeStamp.setText(data.getTimeStamp().format(new Date()));
catch(InterruptedException e){
catch(Exception err){

I have a class defined called WeatherData that has the values that it's looking for. My problem is the point at which the Thread.sleep() is issued. I've tried moving it out of the while loop, and when the applet is run, it executes the Thread.sleep() statement before any other statement, except the resize(). No matter where I put Thread.sleep(), it runs first. When it is in the while loop, it executes once, then nothing else happens. Can anyone explain this to me? Thanks!

Brett Fitzgerald
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Because of the "while(true) {sleep()}", the init() method will never return. This is bad, because init() is called from the same Thread that Java uses to call paint() and also your event handlers. So by making init() run forever, you've effectively blocked Java from running your GUI.

Loops like yours have to be run in their own Thread. This is fairly convenient to do in an applet; the basic idea looks like

[Jess in Action][AskingGoodQuestions]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

If you want to do something every 30 seconds, then java.util.Timer and java.util.TimerTask are your friends. The Timer object is the controller, and it runs in the separate thread that Ernest mentioned. The TimerTask is the runnable piece that is executed every 30 seconds. Or there's also a javax.swing.Timer class that can send an ActionEvent to an ActionListener periodically.
Brett Fitzgerald

Joined: Mar 21, 2006
Posts: 6
Thanks to both of you for such a quick reply. I tried Ernest's method and it worked well. Paul, I'm planning on giving yours a shot as well. That will actually help me with the other program I'm writing, since I have a couple variable-time-length processes, but I need something else to happen every 30 seconds. Another use for threading, but this timer may make it much simpler. Again, thanks to you both!

I agree. Here's the link:
subject: Problems with Thread.sleep()
It's not a secret anymore!