permaculture playing cards*
The moose likes Beginning Java and the fly likes new Thread freezes up program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new Thread freezes up program" Watch "new Thread freezes up program" New topic
Author

new Thread freezes up program

Max Simpson
Ranch Hand

Joined: Sep 23, 2004
Posts: 30
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

Joined: Dec 21, 2004
Posts: 95
hi max
could u post the code, it`s quite hard to interpret the possible reason


sandy
Max Simpson
Ranch Hand

Joined: Sep 23, 2004
Posts: 30
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

Joined: Mar 13, 2004
Posts: 1272
I think the code you put in the StopWatch() constructor belongs in a run() method.


Mike Gershman
SCJP 1.4, SCWCD in process
Max Simpson
Ranch Hand

Joined: Sep 23, 2004
Posts: 30
a few months away from using Java and I keep missing the obvious.. thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new Thread freezes up program