Quick newbie Java programming question (idle, timers)

Discussion in 'Java' started by Starwynd, Nov 22, 2003.

  1. Starwynd

    Starwynd Guest

    Greetings all -

    I am working on programming a very very simple java application for
    fun - I am still learning java, and was curious of the best way to
    accomplish the following task -

    I want to be able to have my Java program run, and have the user be
    able to exit it at any time by typing an exit string ("exit") or
    something of like. But also, I have other things running on timers in
    the backgrounds - it's kind of a virtual reality game. While the game
    is running, I want it to update onscreen with different messages, such
    as "it is now evening" or "it is now noon", but still be able to
    accept user input at any time to terminate the program. I tried adding
    the system.exit(0) line to my timer such as this:

    import java.util.*;
    import java.io.*;


    public class ShortSnowUpdate extends TimerTask
    {
    public ShortSnowUpdate()
    {
    }
    public void run()
    {
    if (KeyboardReader.readInt() != 5)
    {
    System.out.println("it is snowing");
    }
    else
    System.exit(0);



    }

    }


    But this just sits there at the command prompt always waiting for user
    input, and not actually processing the update when it occurs once a
    minute (the timer event is scheduled in the manager thread to run once
    a minute, and display the message "it is snowing" to the screen). It
    obviously exits nicely when "5" is pressed, but it won't continue to
    give me the "it is snowing" message once a minute. I just need
    something that will let the program idle while responding to the
    timers, but also ending if the user inputs the end input (5 in this
    case) while not constantly prompting the user to enter something.Any
    ideas? I know this is probably something pretty simple! :)

    -Starwynd
     
    Starwynd, Nov 22, 2003
    #1
    1. Advertising

  2. (Starwynd) writes:

    > if (KeyboardReader.readInt() != 5)


    Most likely, this is a blocking call. You need to put the message into
    a separate thread from the one you use to read the keyboard.
     
    Tor Iver Wilhelmsen, Nov 23, 2003
    #2
    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. Stephen Inkpen

    Timers in application web programming

    Stephen Inkpen, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    351
    Ken Cox [Microsoft MVP]
    Jul 16, 2003
  2. Dave Hagerich

    newbie quest about System.Timers.Timer

    Dave Hagerich, Dec 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    764
    S. Justin Gengo
    Dec 12, 2005
  3. JKop
    Replies:
    11
    Views:
    937
  4. mike79

    newbie: timers..

    mike79, Nov 8, 2003, in forum: C Programming
    Replies:
    2
    Views:
    357
    EventHelix.com
    Nov 9, 2003
  5. Elaine Jackson

    quick question about IDLE

    Elaine Jackson, Jan 26, 2004, in forum: Python
    Replies:
    1
    Views:
    286
    Kurt B. Kaiser
    Feb 1, 2004
Loading...

Share This Page