This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can i make a program wait? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can i make a program wait?" Watch "Can i make a program wait?" New topic
Author

Can i make a program wait?

MR Chahal
Greenhorn

Joined: Nov 27, 2005
Posts: 14
Hello

Is it possible to make the program wait in java? im running a while loop and i want to make the program wait for 1 second before going through the loop again. How can i do that?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Use the Thread.sleep method.
However, this sounds like the typical busy/wait antipattern.
Is the Object wait/notify more appropriate for your needs insteading of polling for some state change?


Tony Morris
Java Q&A (FAQ, Trivia)
MR Chahal
Greenhorn

Joined: Nov 27, 2005
Posts: 14
Hello Tony

This is what im trying to do. I have a int with a value of 5. I have another int with a value of 1. Im adding 1 + 5 = 6 and getting all the number between 1 and 6 which are 2,3,4 and 5. Im doing this using a while loop. Now i get the number but unfortunately the proccess is over and done with in less than a second. I want it to wait before it goes through the loop again. Im not suere weather to use sleep or wait or what. What would u sujjest?
Mattias Arthursson
Ranch Hand

Joined: Jul 26, 2004
Posts: 90
You should use sleep(), but notice that the argument to that method is the time to sleep in milliseconds. In order to sleep for a second, you need to call sleep(1000).


Admit nothing. Blame everyone. Be bitter.
MR Chahal
Greenhorn

Joined: Nov 27, 2005
Posts: 14
Hello

when i use the sleep function, the program sleep but not exactly like i want it to. When i get the numbers from between 1 and 6, i am just putting them in a textbox. Before the sleep funciton, the whole loop was completed in less than a second. After i used the sleep function, the system wait and then all the numbers appear in the text box.

How can i make it so that it put 2 in the text box, sleeps for 500 miliseconds, then goes through the loop and puts 3 in the textbox, again wait for 500 millisec, go through loop again and put 4 in the textbox like it would be if i used a timer.
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
Put sleep() between these steps...

say

1 Get a number
2 put number in text box
3 sleep(500);
4 goto first

Is it not working ?


Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
MR Chahal
Greenhorn

Joined: Nov 27, 2005
Posts: 14
Hello

No it is not working. This is what my code looks like:
----------------------------
while (int1 != int2) {
tp1c = int1 +1;
//textbox1.setText(""int1); // this is for testing.
try
{
Thread.sleep( 500);
}
catch( InterruptedException e )
{
}

}
-----------------------------

What happens is that it just waits for 500 milliseconds and then executes the code so all i see in textbox1 is 5 instead of 2 then wait for 500 millisecond , then 3, wait for 500 milliseconds, then 4, wait for 500 milliseconds and then finally 5.

what am i doing wrong?
[ April 17, 2006: Message edited by: MR Chahal ]
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
class Test implements Runnable{

Thread t;

public Test(){
t=new Thread(this);
t.start();
}

public void run(){
while (int1 != int2) {
tp1c = int1 +1;
//textbox1.setText(""int1); // this is for testing.
try
{
Thread.sleep( 500);
}
catch( InterruptedException e )
{
}
}
}
public static void main(String arg[]){

new Test();
}

}
 
Consider Paul's rocket mass heater.
 
subject: Can i make a program wait?
 
Similar Threads
Can threads be made to have equal chance of running???
direct connection (Networking)
Thread Synchronization
notifyAll
opening multiple frames in a loop