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
Write a java program to count the number of words present in a string?
What is the protected method modifier?
What is the static keyword?
What is the purpose of assert keyword used in jdk1.4.x?
What are the different collection views provided by maps?
Why is the singleton pattern considered to be an anti pattern?
How do you declare a variable?
Explain the selection sort algorithm and state its time complexity?
How many bits are in a sentence?
Can main() method in java can return any data?
What is java util concurrentmodificationexception?
How to find the largest value from the given array.
Why declare Main() inside the class in java ?
How do you remove duplicates from an array in java?
Compare overloading and overriding?