Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with Thread.sleep()

 
Brett Fitzgerald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
setBackground(Color.black);
lblTimeStamp = new Label("lblTimeStamp");
lblTimeStamp.setAlignment(Label.CENTER);
lblTimeStamp.setFont(new Font("Serif", Font.BOLD, 12));
lblTimeStamp.setForeground(Color.red);

while(true){

try{
Thread.sleep(30000);
FileInputStream fs = new FileInputStream(path);
ObjectInputStream is = new ObjectInputStream(fs);
data = (WeatherData)is.readObject();
lblTimeStamp.setText(data.getTimeStamp().format(new Date()));
os.close();
}
catch(InterruptedException e){
}
catch(Exception err){
System.out.println(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
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Brett
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic