Waiting until timer without cpu crash?

  • Thread starter Alessandro Giambruno
  • Start date
A

Alessandro Giambruno

Hi to all...

Mine is a very stupid problem, but i can't already solve it. A lot of times
i need to make my main class WAIT (until a certain number of seconds or a
change in variable content). I cannot apply an Listener because i don't need
user interaction.
If i make a While(true){ ...break when timer finish... } or
While(timer control) { empty }
i have a "little" problem: my class will use 100% of my Cpu in a cycle
(notwithstanding i do nothing inside the while).

I'd like to have a command like "wait" in old Basic...i cannot solve my
problem in other way.

I don't use multithreading, so i can't imagine to use wait...run commands in
multithreading. Also i think i can find a easier solution

Lots of kisses to all programmers....
Alex
Italy

--


_____________________________________
"Heard melodies are sweet,
but those unheard are sweeter;
therefore, ye soft pipes, play on,
- Not to the sensual ear, but, more endear'd,
Pipe to the spirit ditties of no tone."

(J. Keats, "Ode on a Grecian Urn")
 
J

Joona I Palaste

Alessandro Giambruno said:
Hi to all...
Mine is a very stupid problem, but i can't already solve it. A lot of times
i need to make my main class WAIT (until a certain number of seconds or a
change in variable content). I cannot apply an Listener because i don't need
user interaction.
If i make a While(true){ ...break when timer finish... } or
While(timer control) { empty }
i have a "little" problem: my class will use 100% of my Cpu in a cycle
(notwithstanding i do nothing inside the while).
I'd like to have a command like "wait" in old Basic...i cannot solve my
problem in other way.
I don't use multithreading, so i can't imagine to use wait...run commands in
multithreading. Also i think i can find a easier solution

Look up java.lang.Thread.sleep(int milliseconds).

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
- Douglas Adams
 
C

Chris Smith

I'll join in to say that the equivalent to your BASIC wait command is
Thread.sleep. That said, it may be that you want to consider a
different design. Your application code may be much simplified by using
java.util.Timer instead. In that case, you wouldn't bother to continue
running the main thread; just schedule a TimerTask and then let the main
thread end so the TimerTask can become the driver for your application
from that point onward.

Furthermore, be very cautious about using Thread.sleep in a GUI
application. In that case, you almost certainly want some kind of timer
instead, and quite possible javax.swing.Timer instead of java.util.Timer
as recommended earlier. (The difference is that Swing's Timer will
execute your code in a context where it is safe to interact with GUI
components, whereas the java.util Timer would require you to then use
EventQueue.invokeLater to get into such a context by hand.)

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Joona I Palaste

I would say "instead of your while-loop."

Fat lot of good that will do if he wants to sleep until a specific time
has elapsed, or a variable has changed, whichever comes first. He can't
even use Thread.interrupt() because he doesn't use multithreading. I
suggest using a device we actually used in production code in my
previous job:

int slept = 0;
while (slept < 60000 && !variableHasChanged) {
try {
Thread.sleep(200); slept += 200;
}
catch (InterruptedException ie) {
break;
}
}
if (variableHasChanged) {
/* variable changed in time */
}
else {
/* timed out */
}

This is a busy-loop like the OP is using now, but this one consumes a
*far* smaller share of CPU cycles.
 
A

Alessandro Giambruno

i thought the same: using Thread.sleep in a gui can be hopeless cause, i've
seen that VM automatically creates muloti-threads after importing
JComponents...:S
so u believe if I use Swing.Timer cpu won't be compromitted?
hope less than the infinite while{...}.... using if inside a
child-actionlistener made me mad: the GUI was locked...:S

thanx
alex
 
C

Chris Smith

Alessandro said:
so u believe if I use Swing.Timer cpu won't be compromitted?
hope less than the infinite while{...}.... using if inside a
child-actionlistener made me mad: the GUI was locked...:S

Yes, infinite loops are never better than Thread.sleep, and in fact
always worse. They suffer from the same problem -- they block the event
thread.

Yes, the Timer classes will work for this.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

i thought the same: using Thread.sleep in a gui can be hopeless cause, i've
seen that VM automatically creates muloti-threads after importing
JComponents...:S
so u believe if I use Swing.Timer cpu won't be compromitted?
hope less than the infinite while{...}.... using if inside a
child-actionlistener made me mad: the GUI was locked...:S

See http://mindprod.com/jgloss/thread.html

I think you are making the standard newbie error of putting the GUI
thread to sleep.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top