the same problem...and thanx a lot

Discussion in 'Java' started by Alessandro Giambruno, Jun 19, 2004.

  1. 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")
     
    Alessandro Giambruno, Jun 19, 2004
    #1
    1. Advertising

  2. Alessandro Giambruno wrote:
    > 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) {}
    }
     
    Troy Kinsella, Jun 20, 2004
    #2
    1. Advertising

  3. Alessandro Giambruno wrote:
    > "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.
     
    Michael Borgwardt, Jun 21, 2004
    #3
  4. In article <0QSAc.5239$>,
    "Alessandro Giambruno" <> wrote:

    > 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.
     
    Kevin McMurtrie, Jun 21, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joey Liang via DotNetMonster.com

    Urgently need help in drop down list..thanx

    Joey Liang via DotNetMonster.com, May 24, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    619
    Joey Liang via DotNetMonster.com
    May 24, 2005
  2. =?Utf-8?B?TWlrZUppbmdKaW5n?=

    HELP Access DB, ASP.NET 2.0 and whole lot of pain

    =?Utf-8?B?TWlrZUppbmdKaW5n?=, Dec 6, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    494
    Patrice
    Dec 6, 2005
  3. Found your site in google, and it has a lot of use

    Found your site in google, and it has a lot of usefullinformation. Thanx,some sites

    Found your site in google, and it has a lot of use, May 12, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    298
    Found your site in google, and it has a lot of use
    May 12, 2007
  4. Alex
    Replies:
    1
    Views:
    1,180
    S├ębastien Ouellet
    Aug 29, 2009
  5. Agrapha
    Replies:
    15
    Views:
    177
    Agrapha
    Feb 9, 2004
Loading...

Share This Page