|
-
Apr 15th, 2001, 03:42 PM
#1
Thread Starter
Addicted Member
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
-
Apr 15th, 2001, 07:04 PM
#2
Dazed Member
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;
-
Apr 16th, 2001, 10:33 AM
#3
Hyperactive Member
To get a beep you should need to get the toolkit, try this....
Toolkit.getDefaultToolkit().beep();
-
Apr 16th, 2001, 02:04 PM
#4
Thread Starter
Addicted Member
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.
-
Apr 16th, 2001, 04:25 PM
#5
Dazed Member
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!");
}
}
-
Apr 16th, 2001, 05:13 PM
#6
Dazed Member
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();
}
}
-
Apr 16th, 2001, 05:22 PM
#7
Monday Morning Lunatic
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
-
Apr 18th, 2001, 08:22 AM
#8
Hyperactive Member
Java runs under dos just fine
-
Apr 18th, 2001, 10:01 AM
#9
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.
-
Apr 18th, 2001, 11:16 AM
#10
Monday Morning Lunatic
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
-
Apr 18th, 2001, 02:31 PM
#11
Hyperactive Member
why would you do that?? What do you gain from it?
-
Apr 18th, 2001, 04:48 PM
#12
Monday Morning Lunatic
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
-
May 11th, 2001, 03:24 PM
#13
Frenzied Member
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.
-
May 11th, 2001, 05:45 PM
#14
Dazed Member
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.
-
May 12th, 2001, 06:09 AM
#15
Monday Morning Lunatic
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
-
May 12th, 2001, 10:55 AM
#16
Dazed Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|