Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to use thread sleep in Java

4 Answers

0 votes
public class UseThreadSleep_Java {
    public static void main(String args[]) {
        try {
            long start = System.currentTimeMillis();
            Thread.sleep(1000);
            System.out.println("Sleep time in ms = " + (System.currentTimeMillis() - start));
 
        } catch (InterruptedException ie) {
            System.out.println(ie);
        }
    }
}

 
 
/*
run:
 
Sleep time in ms = 1000
 
*/    

 



answered Mar 24, 2021 by avibootz
edited Sep 1, 2024 by avibootz
0 votes
import java.util.concurrent.TimeUnit;

public class MyClass {
    public static void main(String[] args) {
        try {
            for (int i = 1; i < 6; i++) {
                System.out.println(i);
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (InterruptedException ie) {
            System.out.println("Thread error: " + ie);
        }
    }
}
  
  
  
  
/*
 
run:
 
1
2
3
4
5
 
*/

 



answered Mar 24, 2021 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        Thread mainThread = Thread.currentThread();
          
        System.out.println("Current thread: " + mainThread);
          
        try {
            long start = System.currentTimeMillis();
            Thread.sleep(2000);
            System.out.println("Sleep time in ms = " + (System.currentTimeMillis() - start));
        }
        catch (InterruptedException ie) {
            System.out.println("Exception: " + ie);
        }
    }
}
  
  
  
  
/*
run:
  
Current thread: Thread[main,5,main]
Sleep time in ms = 2000
  
*/

 



answered Mar 25, 2023 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        Thread mainThread = Thread.currentThread();
         
        System.out.println("Current thread: " + mainThread);
         
        mainThread.setName("MyThread");
        System.out.println("Current thread: " + mainThread);
         
        try {
            for (int i = 1; i <= 7; i++) {
                System.out.println("i = " + i);
                Thread.sleep(500);
            }
        }
        catch (InterruptedException e) {
            System.out.println("Exception: " + e);
        }
    }
}
 
 
 
 
/*
run:
 
Current thread: Thread[main,5,main]
Current thread: Thread[MyThread,5,main]
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
 
*/

 



answered Mar 25, 2023 by avibootz

Related questions

1 answer 131 views
131 views asked Jan 29, 2024 by avibootz
1 answer 191 views
1 answer 103 views
103 views asked Sep 1, 2024 by avibootz
1 answer 88 views
88 views asked Sep 1, 2024 by avibootz
1 answer 97 views
97 views asked Sep 1, 2024 by avibootz
1 answer 96 views
96 views asked Sep 1, 2024 by avibootz
...