How to pause thread using sleep method in Java

1 Answer

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

 



answered Oct 14, 2016 by avibootz
edited Mar 24, 2021 by avibootz

Related questions

2 answers 171 views
171 views asked Nov 9, 2021 by avibootz
1 answer 141 views
141 views asked Jan 29, 2024 by avibootz
4 answers 2,721 views
2,721 views asked Mar 24, 2021 by avibootz
1 answer 223 views
...