- /*
- Pause Thread Using Sleep Method Example
- This Java example shows how to pause currently running thread using
- sleep method of Java Thread class.
- */
- public class PauseThreadUsingSleep {
- public static void main(String[] args) {
- /*
- * To pause execution of a thread, use
- * void sleep(int milliseconds) method of Thread class.
- *
- * This is a static method and causes the suspension of the thread
- * for specified period of time.
- *
- * Please note that, this method may throw InterruptedException.
- */
- System.out.println("Print number after pausing for 1000 milliseconds");
- try{
- for(int i=0; i< 5; i++){
- System.out.println(i);
- /*
- * This thread will pause for 1000 milliseconds after
- * printing each number.
- */
- Thread.sleep(1000);
- }
- }
- catch(InterruptedException ie){
- System.out.println("Thread interrupted !" + ie);
- }
- }
- }
- /*
- Output of this example would be
- Print number after pausing for 1000 milliseconds
- 0
- 1
- 2
- 3
- 4
- */
Welcome to the world of Java examples, organized by categories and Java packages. Java examples (Java sample source code) help to understand functionality of various Java classes and methods as well as various programming techniques in a simple way, which is otherwise very hard to learn by reading tutorials or Java API. So start exploring...
Sunday, November 27, 2011
Pause Thread Using Sleep Method Example
Subscribe to:
Post Comments (Atom)
How to find the most appropriate Keywords?
🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...
-
/* Pause Thread Using Sleep Method Example This Java example shows how to pause currently running thread using sleep method of Jav...
-
/* Java String split example. This Java String split example describes how Java String is split into multiple Java String objects. */ ...
-
// File Name GreetingClient.java import java.net.*; import java.io.*; public class GreetingClient { public static void main(String [] ...
gracias, thanks
ReplyDelete