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.example.lang;
public class ThreadSleepDemo implements Runnable {
// 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...");
}
public static void main(String[] args) {
Thread thread = new Thread(new ThreadSleepDemo());
thread.start();
}
}
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020