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

Write down program for following scenario. Use java coding standard. You have array list with some words in it..we will call it as dictionary….and you have a arbitrary string containing some chars in it. You have to go through each word of dictionary and find out if that word can be constructed with the help of chars from arbitrary string given. If you find the word print it else print none.

0 Answers   Rolta,


How to transfer data from an Applet to Servlet ?

1 Answers  


Is there any need to import java.lang package?

13 Answers   Sun Microsystems,


What state is a thread in when it is executing?

0 Answers  


Can we assign the reference to this variable?

0 Answers  






Why string is not thread safe?

0 Answers  


Is 0 an even number?

0 Answers  


Same common question what is Map,Set,HashMap,List????

4 Answers   Symphony,


Is age a discrete variable?

0 Answers  


Does java allow overriding static methods ?

0 Answers  


Why parsing is done?

0 Answers  


What are different types of classloaders?

0 Answers  


Categories