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

Hi friends am new to java. I read jar file means collection of java files. For executing struts application what are the necessary jar files. " struts.jar " file contains what. can u explain

0 Answers  


How can you sort dates?

2 Answers   TCS,


What is treeset in java?

0 Answers  


What are the two ways to create a thread?

0 Answers  


What is singleton math?

0 Answers  






What is lazy activation?

2 Answers   CMC,


why using interface interface ?

0 Answers  


What is user defined exception in Java?

0 Answers   TCS,


What is Restrictions in hibernate?

1 Answers   Bally Technologies,


What are different types of arrays?

0 Answers  


Can an object?s finalize() method be invoked while it is reachable?

2 Answers  


Can I overload to string method

0 Answers   UHG,


Categories