1using System.Threading;
2
3static void Main()
4{
5 //do stuff
6 Thread.Sleep(5000) //will sleep for 5 sec
7}
1import java.util.concurrent.TimeUnit
2
3TimeUnit.SECONDS.sleep(1);
4or
5TimeUnit.MINUTES.sleep(1);
1
2package com.journaldev.threads;
3
4public class ThreadSleep {
5
6 public static void main(String[] args) throws InterruptedException {
7 long start = System.currentTimeMillis();
8 Thread.sleep(2000);
9 System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start));
10
11 }
12
13}
14
1import java.util.concurrent.TimeUnit
2
3try {
4 TimeUnit.SECONDS.sleep(1);
5}
6catch (Exception e) {
7 System.out.println("Oops! Something went wrong!")
8}