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
Why charat is used in java?
How will you serialize a singleton class without violating singleton pattern?
What is the use of parse function in java?
How to restrict a member of a class from inheriting by its sub classes?
Which package has light weight components?
What is protected access modifier?
What is object data type?
Hi Anyone know the model / questions of the Federal bank sample questions for the post of Specialist Officers - Programmers. Please post if anyone have..
Explain what is encapsulation?
What is Gang of four design patterns
Describe what happens when an object is created in java ?
Which class represents the socket that both the client and server use to communicate with each other?
How do you remove an object from an arraylist in java?
What is the difference between pass by reference and pass by pointer?
Explain implementation and how is it different from conversion?