the same problem...and thanx a lot

  • Thread starter Alessandro Giambruno
  • Start date
A

Alessandro Giambruno

Hi to all...
and first of all THANX to have answered me last time...
I had this problem:
"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...."

and u answered me that Thread.sleep(m) could be a solution.
I need another thing:
is there a method like Sleep but that finishes his "sleeping" after a
CHANGE of VARIABLE CONTENT? (i.e. int a-->sleep-->a=2-->run)


THANX A LOOT


_____________________________________
"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")
 
T

Troy Kinsella

Alessandro said:
Hi to all...
and first of all THANX to have answered me last time...
I had this problem:
"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...."

and u answered me that Thread.sleep(m) could be a solution.
I need another thing:
is there a method like Sleep but that finishes his "sleeping" after a
CHANGE of VARIABLE CONTENT? (i.e. int a-->sleep-->a=2-->run)


THANX A LOOT


_____________________________________
"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")

Sounds to me like a better design would be to use multiple threads.
Perhaps you could have a polling thread to see if content changes (or
whatever), and another thread that wait()s until it is notify()ed by the
polling thread.

Or you could just have one thread that checks the "content changed"
status every so often, and wait()s inbetween checks. This way, your
while loop wouldn't consume 100% CPU.

i.e.

while (!contentChanged() && !timedOut()) {
try {
synchronized (this) {
wait(200);
}
} catch (InterruptedException ie) {}
}
 
M

Michael Borgwardt

Alessandro said:
"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 don't use multithreading, so i can't imagine to use wait...run commands in
multithreading.

You *are* using multithreading, otherwise there could be no change in variable contet.
 
K

Kevin McMurtrie

Alessandro Giambruno said:
Hi to all...
and first of all THANX to have answered me last time...
I had this problem:
"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...."

and u answered me that Thread.sleep(m) could be a solution.
I need another thing:
is there a method like Sleep but that finishes his "sleeping" after a
CHANGE of VARIABLE CONTENT? (i.e. int a-->sleep-->a=2-->run)


THANX A LOOT

Learn proper threading. You're a fool if you think you can ignore
threading or hack your way around them.

Object.wait()/Object.notify() does exactly what you're looking for.
You'll need to learn at least the threading basics to make them work.
 

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

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top