Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Write a java program to count the number of words present in a string?

1020


What is the protected method modifier?

1086


What is the static keyword?

1141


What is the purpose of assert keyword used in jdk1.4.x?

1122


What are the different collection views provided by maps?

1041


Why is the singleton pattern considered to be an anti pattern?

942


How do you declare a variable?

1138


Explain the selection sort algorithm and state its time complexity?

1068


How many bits are in a sentence?

1256


Can main() method in java can return any data?

1236


What is java util concurrentmodificationexception?

956


How to find the largest value from the given array.

1066


Why declare Main() inside the class in java ?

1114


How do you remove duplicates from an array in java?

1009


Compare overloading and overriding?

1056