Results 1 to 16 of 16

Thread: Delay

  1. #1

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    Hi, does any one know if there is a delay function in Java you know like the one you get in Turbo Pascal

    Delay (1000);

    I don't want to write a loop which iterates for a long time to produce the delay because that speed depends on PC processor, and I want the speed to be consistent across different PCs

    Also is there a beep feature in Java?

    THANKS IN ADVANCE

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    try using threads in your program.

    The wait() and notify() methods are not of the thread
    class but of the Object class. Every Java Object has
    a lock associated with it. Every object can maintain a
    list of waiting threads.


    When a thread calls the wait() method of an Object, it is added to a list of waiting threads for that object and
    stops running. When another thread calls the notify()
    method of the same object() the object wakes up one
    of the waiting threads and alloes it to continue running.

    sleep can also be used:

    public static void sleep(long millis) throws
    InterruptedException;

    public static void sleep(long millis, int nanos) throws InterruptedException;

  3. #3
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    To get a beep you should need to get the toolkit, try this....


    Toolkit.getDefaultToolkit().beep();

  4. #4

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    Well I haven't got as far as thread yet. I new to Java so need somthing a bit simpler. The program I'm coding runs under the DOS command prompt and thus is not a GUI program so the answers I need have to work under DOS.

    billrogers, I haven't as yet tried you answer but would it work under dos?
    Last edited by Ramandeep; Apr 16th, 2001 at 02:19 PM.

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    As far as i know threads will run under DOS.


    class threadtest{
    public static void main(String[] args){

    for(int i = 0; i <= 9; i++){
    System.out.println("loop " + i);
    }

    Thread t = new sleeper();

    try{
    t.sleep(5000);
    sleeper.imsleepy();

    }catch(InterruptedException e) {System.err.println(e);}

    }
    }

    class sleeper extends Thread{

    public static void imsleepy(){
    System.out.println("Im sleepy method just slept for 5 seconds!");
    }
    }

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    you can also use the Timer and TimerTask class of the java.util package..

    class TimerTest{
    public static void main(String[] args){

    // format the time
    final java.text.DateFormat timeFmt = java.text.DateFormat.getTimeInstance(java.text.DateFormat.MEDIUM);

    // define the time display task

    TimerTask displayTime = new TimerTask(){
    public void run() {System.out.println(timeFmt.format(new Date())); }
    };

    // create a timer object to run task

    Timer timer = new Timer();
    timer.schedule(displayTime, 0, 1000);

    // to stop the time-display task

    displayTime.cancel();
    }
    }

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Java can't run under DOS without some severe trickery either.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Java runs under dos just fine

  9. #9
    VirtuallyVB
    Guest

    Question What's with the "No DOS" comments?

    Do you mean a DOS shell from within Windows or a standalone DOS like version 6.2 or less? I have no problems developing from DOS, sometimes I prefer it to a full blown IDE, but I'm using a DOS shell from within Windows.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I mean actually booting into DOS, rather than DOS/Windows.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    why would you do that?? What do you gain from it?

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I don't know. I was just giving factual answers to the question of whether Java would run under DOS.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Thumbs down

    You don't need all this code:
    Code:
    
    class threadtest{ 
    public static void main(String[] args){ 
    
    for(int i = 0; i <= 9; i++){ 
    System.out.println("loop " + i); 
    } 
    
    Thread t = new sleeper(); 
    
    try{ 
    t.sleep(5000); 
    sleeper.imsleepy(); 
    
    }catch(InterruptedException e) {System.err.println(e);} 
    
    } 
    } 
    
    class sleeper extends Thread{ 
    
    public static void imsleepy(){ 
    System.out.println("Im sleepy method just slept for 5 seconds!"); 
    } 
    }
    
    Or all of this:
    Code:
    
    class TimerTest{ 
    public static void main(String[] args){ 
    
    // format the time 
    final java.text.DateFormat timeFmt = java.text.DateFormat.getTimeInstance(java.text.DateFormat.MEDIUM); 
    
    // define the time display task 
    
    TimerTask displayTime = new TimerTask(){ 
    public void run() {System.out.println(timeFmt.format(new Date())); } 
    }; 
    
    // create a timer object to run task 
    
    Timer timer = new Timer(); 
    timer.schedule(displayTime, 0, 1000); 
    
    // to stop the time-display task 
    
    displayTime.cancel(); 
    } 
    }
    
    This is all you need:
    Code:
    
    try{Thread.sleep(1000);} 
    catch (InterruptedException e){}
    
    The program will pause for 1 sec.
    In an applet or console (DOS) program.

  14. #14
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yes while you are right........ The purpose was to show how the
    java.util.Timer and java.utilTimerTask class work... We all know that a task can ususlly be accomplished with less code but it sometimes lacks a clear understanding.

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Firstly, just re-read the thread from the top. The answer (sleep) was given very early on, and everything else was an explanation of using threads.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    No need to get upset...... Obviously he didnt ask how the java.util.Timer and java.util.TimerTask class work but what's so bad about pointing out another alternative? And since you obviously read the entire post to see that he didnt ask about the
    "java.util.Timer and java.util.TimerTask classes" then you must of noticed that the answer sleep was given early on as parksie so kindly pointed out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width