• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

new Thread freezes up program

 
Max Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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?
 
shandilya popuru
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • 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
  • 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
}
}
}
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
a few months away from using Java and I keep missing the obvious.. thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic