This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Question on StackOverflow error
JFrame freezing when a Start button is added
Regarding garbage collector