devxlogo

Java Pausing Abilities

Java Pausing Abilities

Question:
I want to make my Java program pause for a specified amount of time, but I don’t want to use a threading implementation. How can I do this?

Answer:
You really cannot avoid using threads in Java because the Java virtual machine automatically starts at least two separate threads. One thread is the low priority garbage collection thread and the other thread is the one that runs your main program. Graphical programs may automaticallystart a third or even additional threads to manage the GUI.

To pause your program for a specified amount of time, all you have todo is call the static method Thread.sleep(),which will suspend the calling thread forthe specified number of milliseconds.To pause your program indefinitely, youmust get a handle to the main thread and callits suspend() method. You can thenresume the thread at a later time by callingthe resume() method from a separate thread.

devx-admin

Share the Post: