how to print hello world every second till i have pressed
enter key ???



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

Post New Answer

More Core Java Interview Questions

What is remote method invocation (rmi)?

0 Answers  


How strings are created in java?

0 Answers  


Are the equals() and hashCode() protected methods of object class?

1 Answers   Avaya,


how can you say that java is independ language

2 Answers   TCS, Wipro,


What is t type java?

0 Answers  






What is an array in java?

0 Answers  


What are methods in java?

0 Answers  


Can inner class final?

0 Answers  


What is the abstraction?

0 Answers  


What is ‘has a’’ relationship in java?

0 Answers  


What is final, finally, finalize?

0 Answers  


What is continuity of a function?

0 Answers  


Categories