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
Can we rethrow the same exception from catch handler?
Is hashset ordered java?
Can we restart a thread already started in java?
What is the concept of multithreading?
Why java is said to be pass-by-value ?
Tell us something about set interface.
How to make a read-only class in java?
Why do we need autoboxing in java?
How to perform quicksort in java?
What are the six ways to use this keyword?
What are the important methods of java exception class?
What is difference between fail-fast and fail-safe?
What is the difference between import java.util.date and java .util?
What is core java used for?
what is difference betweem home interface and remote interface?