• 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

new Thread freezes up program

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have an application that has a stopwatch that starts in its own thread. I have the thread sleep for 5 seconds, then display the amount of time that has passed. Problem is, that the timer runs in a loop and essentially freezes up the program while it runs.

Since it is in its own thread and sleeps, this shouldnt happen, right?
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi max
could u post the code, it`s quite hard to interpret the possible reason
 
Max Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my main class, I call:

Thread t = new StopWatch();
t.setPriority(1); //tried different priorities.. no effect
t.start();

class StopWatch extends Thread{
public StopWatch(){
for(int i=0;i<60;i++){
try{
sleep(1000);
}catch(Exception e){}
long after = System.currentTimeMillis();
long timespent = after - before; //in milliseconds
stopWatchField.setText("" + timespent/1000);
MyApp.getMyPanel().repaint(); //repaint the display
}
}
}
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the code you put in the StopWatch() constructor belongs in a run() method.
 
Max Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a few months away from using Java and I keep missing the obvious.. thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic