how to print hello world every second till i have pressed
enter key ???
Answer / 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 |
Do extraneous variables affect validity?
what is overloading and overriding with example?
Why deletion in linkedlist is fast than arraylist?
Is java supports multiple inheritance? explain?
How concurrent hashmap works?
When should we create our own custom exception classes?
Is there any way to skip finally block of exception even if some exception occurs in the exception block?
What is difference overloading and overriding?
what are the rules to use try catch finally?
What is main in java?
Can we define a package statement after the import statement in java?
Difference between the String and StringBuffer classes?