Lately I’ve been using loops with large numbers to print out Hello World:
int counter = 0;
while(true) {
//loop for ~5 seconds
for(int i = 0; i < 2147483647 ; i++) {
//another loop because it's 2012 and PCs have gotten considerably faster :)
for(int j = 0; j < 2147483647 ; j++){ ... }
}
System.out.println(counter + ". Hello World!");
counter++;
}
I understand that this is a very silly way to do it, but I’ve never used any timer libraries in Java yet. How would one modify the above to print every say 3 seconds?
You can also take a look at
TimerandTimerTaskclasses which you can use to schedule your task to run everynseconds.You need a class that extends
TimerTaskand override thepublic void run()method, which will be executed everytime you pass an instance of that class totimer.schedule()method..Here’s an example, which prints
Hello Worldevery 5 seconds: –