File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

new Thread freezes up program

Max Simpson
Ranch Hand

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

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

class StopWatch extends Thread{
public StopWatch(){
for(int i=0;i<60;i++){
}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:
subject: new Thread freezes up program
Similar Threads
Java freezes?!?! Help please
JFrame freezing when a Start button is added
Question on StackOverflow error
Regarding garbage collector