Home arrow static arrow Java Programming [Archive] - console combined with sleep command
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - console combined with sleep command
This topic has 5 replies on 1 page.

Posts:64
Registered: 12/23/03
console combined with sleep command  
Aug 1, 2004 5:15 AM



 
what i want to do is this :
i want a console to wait for my command
and on a key pressed to check the key and call the relevent function
at the same time i want a certain function to happen every 10 minutes for exaple|!
is this possible?
thanks in advance
peleg
 

Posts:8,813
Registered: 10/4/00
Re: console combined with sleep command  
Aug 1, 2004 7:43 AM (reply 1 of 5)



 
Java doesn't do keystrokes on the console very well. If you want to trap keystrokes a GUI would be a good idea. Her's something along the lines you are asking for.
import java.io.*;public class Test3 {  public static void main(String[] args) {    new Thread(new Runnable() {      public void run() {        while (true) {          try { Thread.sleep(10000); } catch (Exception e) {}          System.out.println("10 seconds");        }      }    }).start();    try {      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));      while (true) {        System.out.print("Input? ");        String line = in.readLine();        System.out.println("You typed: "+line);        if (line.equalsIgnoreCase("quit")) break;      }    } catch (IOException ioe) {}    System.exit(0);  }}
 

Posts:24,517
Registered: 98-02-27
Re: console combined with sleep command  
Aug 1, 2004 7:53 AM (reply 2 of 5)



 
i want a console to wait for my command and on a key pressed to check the key and call the relevent function

create a loop:

while (key != programStop)
{
do something
}

at the same time i want a certain function to happen every 10 minutes for exaple|!

Use a Timer. Read the API for more information.
 

Posts:64
Registered: 12/23/03
Re: console combined with sleep command  
Aug 1, 2004 11:21 PM (reply 3 of 5)



 
bbrita can u pleae tell me why do u do :
while (true) {....}

and why isnt the Run() method is called every X seconds?
 

Posts:8,813
Registered: 10/4/00
Re: console combined with sleep command  
Aug 1, 2004 11:45 PM (reply 4 of 5)



 
bbrita can u pleae tell me why do u do :
while (true) {....}

It's a thread. See here.
http://java.sun.com/docs/books/tutorial/essential/threads/definition.html

When you call a thread's start() method, it creates an actual thread and calls its run() method.

I wanted the thread to run until the program ended (System.exit(0)) so I put the thread in an infinite loop.

The loop prints a string, waits for 10 seconds, and starts over, printing the same string, waiting for ten seconds, yadita, yadita

and why isnt the Run() method is called every X
seconds?

I hope the above made it clear. You can read more about threads and timers here
http://java.sun.com/docs/books/tutorial/essential/threads/index.html
 

Posts:64
Registered: 12/23/03
Re: console combined with sleep command  
Aug 2, 2004 1:24 AM (reply 5 of 5)



 
thanks again
i have used your code like this 'and maybe u can help me with a thing or two :
import java.io.*; public class colorsMain {    public static void main(String[] args) {        final daromConnect myCon = new daromConnect();        final long pNextTime = 100000;        new Thread(new Runnable() {            public void run() {                while (true) {                    try {                        Thread.sleep(pNextTime);                    } catch (Exception e) {                    }                    System.out.println("10 seconds");                    myCon.getDaromData();                 }            }        }).start();        try {            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));            while (true) {                System.out.print("Input? ");                String line = in.readLine();                System.out.println("You typed: " + line);                if (line=="start") myCon.getDaromData();                if (line.equalsIgnoreCase("quit")) break;            }        } catch (IOException ioe) {        }        System.exit(0);    }}

i have a class called "daromConnect"
what i dont understand is why:
1) why must i decalre this as final :
final daromConnect myCon = new daromConnect();
2) why can't i make pNextTime as a variable that i change it's time and it will
be used with : Thread.sleep(pNextTime); -->> so i will make the sleep every time as another time i want
3)and last why when i run
if (line=="start") myCon.getDaromData(); 

its like the myCon isn't activating the getDaromData function (while i know that line=="start")
and in the line : myCon.getDaromData(); in theRun() method it does work!
thanks for any help
PEleg
 
This topic has 5 replies on 1 page.