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 one thread block the other thread?
What is string in java with example?
Which is easier .net or java?
How big is a pointer?
What is run time allocation?
Can singleton class be inherited in java?
What is the static keyword?
How to store image in arraylist in java?
What is integer size in java?
State two differences between C and Java.
What comes to mind when someone mentions a shallow copy in java?
What is return type in java?
How do you add an arraylist to an array in java?
What do you understand by a Static Variable?
What are the four integer types supported by java?