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 sleep a thread in Java

1 Answer

0 votes
class MyThread implements Runnable {
    public void run() {
        try {
            for (int i = 1; i <= 3; i++) {
                System.out.println("Thread " + Thread.currentThread().getId() + " is running");
    
                Thread.sleep(2000);
            }
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
 
public class Program {
    public static void main(String[] args) {
        Thread trd1 = new Thread(new MyThread());
        Thread trd2 = new Thread(new MyThread());
        Thread trd3 = new Thread(new MyThread());
 
        trd1.start();
        trd2.start();
        trd3.start();
    }
}
 
 
  
/*
run:
  
Thread 17 is running
Thread 15 is running
Thread 16 is running
Thread 17 is running
Thread 15 is running
Thread 16 is running
Thread 17 is running
Thread 15 is running
Thread 16 is running
  
*/

 



answered Jan 29, 2024 by avibootz

Related questions

4 answers 2,695 views
2,695 views asked Mar 24, 2021 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
...