I need a variable to count upwards but in increments of 2 seconds. Right now I'm simply using a ++;
function, but as you know it's extremely fast.
Is there anything simple I can use to count at a slower pace?
I need a variable to count upwards but in increments of 2 seconds. Right now I'm simply using a ++;
function, but as you know it's extremely fast.
Is there anything simple I can use to count at a slower pace?
Thread.sleep(2000);
This will make your program to wait for 2 seconds between this method call and whatever line of execution immediately follows this.
public class Count implements Runnable{
public void run(){
for(int i=0;i<=6;i+=2){
Thread.sleep(2000)//in milliseconds ...sleeping for 2 sec
sysout(...);//print your value
}
}
}
Start it this way
Runnable r=new Count();
Thread t=new Thread(r);
t.start(); // start the thread
What you doing is basicly making a thread and running with a delay.I hope you get a concept
Yes, you can pause the execution for two seconds by using Thread.sleep(2000).
//Your code...
Thread.sleep(2000);
counter = counter + 2;
//Your code...
This will print from 1 to 99, incrementing by 2 with a one second pause between increments.
public static void main(String[] args) {
for (int i = 1; i < 100; i += 2) { // i = i + 2
System.out.printf("i = %d\n", i); // print i = #
try {
Thread.sleep(2000); // two seconds.
} catch (InterruptedException e) {
}
}
}