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 |
What is meant by packages?
What is package protected in java?
What are local variables?
What is the argument type of main() method?
What is public static void main?
Name the package that always imported by default?
Explain the use of javap tool.
What is the main method java?
What is difference between identifier and variable?
What does the “static” keyword mean?
How we can make copy of a java object?
What is the difference between abstraction and encapsulation?