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
Why vector is used in java?
Can vector have duplicates in java?
What does jenkins do?
What is the integer of 16?
Why to use nested classes in java? (Or) what is the purpose of nested class in java?
Explain about static imports in java?
What are the files generated after using IDL to java compiler?
What are examples of modifiers?
How many characters is 2 bytes?
can any body body expalin best definitions & best real time exaples for opps concepts.
How do I run java on windows?
Can a function return a function?
What are JVM.JRE, J2EE, JNI?
What is nullpointerexception in java?
What is double data type?