how to print hello world every second till i have pressed
enter key ???
Answer Posted / core
import java.util.Scanner;
public class InfiniteThread extends Thread {
public void run() {
while (true) {
System.out.println("hi");
try {
this.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
new ExitThread().start();
new InfiniteThread().start();
}
}
class ExitThread extends Thread{
@Override
public void run() {
Scanner scan = new Scanner(System.in);
String next = scan.nextLine();
if(next.equals(""))
System.exit(0);
super.run();
}
}
| Is This Answer Correct ? | 15 Yes | 4 No |
Post New Answer View All Answers
What is the return type of the main method?
Is java a prime method?
Why java is said to be pass-by-value ?
Why isn’t there operator overloading?
Is void a return type?
Name four container classes.
What is math floor in java?
What is the purpose of javac exe?
What are the different ways of creating thread?
Is binary a low level language?
What should I import for arraylist in java?
What does exclamation mean in java?
What are wrapped classes in java programming?
I want to persist data of objects for later use. What is the best approach to do so?
What is factor r?