loops - Thread.sleep(); in Java -


whenever use thread.sleep(); in while loop, hints tell me, "invoking thread.sleep in loop can cause performance problems." have heard many other websites , books. can use instead?

here's code:

import javax.swing.*;  public class delay {      public static void main(string[] args) throws exception {         int difficulty;         difficulty = integer.parseint(joptionpane                 .showinputdialog("how you?\n" + "1 = evil genius...\n"                         + "10 = evil, not genius"));         boolean cont;         {             cont = false;             system.out.println("12");             thread.sleep(500);              string again = joptionpane.showinputdialog("play again?");             if (again.equals("yes"))                 cont = true;         } while (cont);     } } 

try java.util.timer and/or javax.swing.timer. play them bit, set initial delay, repetition period, etc. see suits needs.

be sure check differences between these 2 timers, starters take @ question: why there 2 timer classes in java(one under javax.swing, 1 under java.util )?

then try scheduledexecutorservice, suggested @boristhespider.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -