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 |
Does list maintain insertion order java?
What is meant by Servelet? What are the parameters of service method?
Is it possible to declare abstract class as final?What happens if we do so?
What is command line used for?
I was asked to draw the class diagram for the below scenario which should obey OOPS concept. A Portal is to be developed for a school , which has 3 main divisions viz , Education , Admin & Trust. Each division has 2 sub divisions Kinder Garden & Higer Secondary.
What is an infinite loop? How infinite loop is declared?
How can you share data between two thread in Java?
What's the base class of all exception classes?
Write the program numbers into words.For example 2345==two thousand three hundred fourty five
Can we have a method name same as class name in java?
How can we find the sum of two linked lists using stack in java?
What are local variables?