• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with Thread.sleep()

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic