Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip: Infinite Loop Using Java

Learn why the "while(true)" command is an easier way to achieve the same result.




Application Security Testing: An Integral Part of DevOps

Certain aspects of your code may want to perform certain activities in a loop and wait infinitely for a command. However, the "while(true)" command is an easier way to achieve the same result.

public class InfiniteLoop
   public static void main(String args[])
      InfiniteLoop infiniteLoop = new InfiniteLoop();
   private void proceed()
      System.out.println("Start of infinite loop");
      boolean condition = true;
      while (condition)
         System.out.println("In loop");
         //You can perfrom the actual work such as accepting unlimited requests and 
         //process them in a newly spawned thread or from a ThreadPool
            //Introducing a small delay for you to make it visible
         }catch(InterruptedException ie)
            System.out.println("InterruptedException: "+ ie);
      //The below line cannot be reached since the above loop is infinite. 
      //However, there are other mechanisms to make it work if you can enhance your code to handle interruption
      System.out.println("End of infinite loop");


Visit the DevX Tip Bank


Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date