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

Can we rethrow the same exception from catch handler?

1107


Is hashset ordered java?

1068


Can we restart a thread already started in java?

1090


What is the concept of multithreading?

978


Why java is said to be pass-by-value ?

1022


Tell us something about set interface.

984


How to make a read-only class in java?

1139


Why do we need autoboxing in java?

979


How to perform quicksort in java?

1092


What are the six ways to use this keyword?

1051


What are the important methods of java exception class?

1087


What is difference between fail-fast and fail-safe?

1222


What is the difference between import java.util.date and java .util?

1042


What is core java used for?

1102


what is difference betweem home interface and remote interface?

2610