You can pause a current thread for a number of milliseconds by using the sleep()
method of the Thread
class. While the current thread is sleeping, it will allow other threads to execute.
package org.kodejava.lang;
public class ThreadSleepDemo implements Runnable {
public static void main(String[] args) {
Thread thread = new Thread(new ThreadSleepDemo());
thread.start();
}
// The run() method will be invoked when the thread is started.
public void run() {
System.out.println("Start..");
try {
// Wait for 10 seconds
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Finish...");
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024