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


Please Help Members By Posting Answers For Below Questions

Why vector is used in java?

554


Can vector have duplicates in java?

505


What does jenkins do?

484


What is the integer of 16?

533


Why to use nested classes in java? (Or) what is the purpose of nested class in java?

521






Explain about static imports in java?

595


What are the files generated after using IDL to java compiler?

582


What are examples of modifiers?

508


How many characters is 2 bytes?

526


can any body body expalin best definitions & best real time exaples for opps concepts.

1837


How do I run java on windows?

525


Can a function return a function?

548


What are JVM.JRE, J2EE, JNI?

682


What is nullpointerexception in java?

521


What is double data type?

537