Achiever
Achiever

Reputation: 1636

How does sleep() method work on given thread and output?

I understand that sleep() is used to sleep a thread for specified time. I made tow examples - in example 1 I am getting output as 1,2, 3,4 because I created only one. In example 2, I created 2 instances of the thread and I'm getting output 1,1,2,2,3,3,4,4.

Why the output is not 1,2, 3, 4 for the first thread followed by 1,2,3,4 for the second one?.

Example 1:

// Using sleep() method
  public class Aaa extends Thread{
    public void run(){
        for(int i=1;i<5;i++){
        try{
           Thread.sleep(500); 
        } catch(InterruptedException e){
            System.out.println(e);
        }
        System.out.println(i);
        }
    }
    public static void main(String[] args){
        Aaa m1=new Aaa();
        m1.start();
    }
  }

Output:
    1
    2
    3
    4

Example 2:

    // Using sleep() method
  public class Aaa extends Thread{
    public void run(){
        for(int i=1;i<5;i++){
        try{
           Thread.sleep(500); // sleeps thread
        } catch(InterruptedException e){
            System.out.println(e);
        }
        System.out.println(i);
        }
    }
    public static void main(String[] args){        
       Aaa m1=new Aaa(); // creating one object
       Aaa m2=new Aaa(); // creating second object of a class
       m1.start(); // calls run method
       m2.start();
    }
  }


Output:
    1
    1
    2
    2
    3
    3
    4
    4

Upvotes: 9

Views: 7703

Answers (5)

AVINASH SHRIMALI
AVINASH SHRIMALI

Reputation: 580

sleep will suspend the execution for the specific time period. This is the efficient way to make the cpu available for other threads to run in parallel. Sleep will not release the lock. But need to take care that invoking sleep doesn't mean that the thread will be suspended for the time given.

Upvotes: 0

user3766199
user3766199

Reputation: 111

To get your desired output 1,2, 3, 4, 1, 2, 3, 4 use the join() method. You can control the thread execution using this method.

Aaa m1=new Aaa(); // creating one object
Aaa m2=new Aaa(); // creating second object of a class
m1.start(); // calls run method
m1.join();
m2.start();

Upvotes: 0

OldCurmudgeon
OldCurmudgeon

Reputation: 65851

You have created two Runnable objects. If you run them by calling their run method you would get what you imagine:

   Aaa m1=new Aaa(); // creating one object
   Aaa m2=new Aaa(); // creating second object of a class
   System.out.println("Calling m1.run()");
   m1.run(); // we call run method
   System.out.println("Calling m2.run()");
   m2.run();

Output

Calling m1.run()
1
2
3
4
Calling m2.run()
1
2
3
4

because the method executes twice, one after the other.

Note that calling the run method of a Runnable/Thread is not the normal way to run a thread. You would normally use the start method.

If, however, you put each one in a Thread and start them:

   Aaa m1=new Aaa(); // creating one object
   Aaa m2=new Aaa(); // creating second object of a class
   System.out.println("Calling m1.start()");
   m1.start(); // thread calls run method
   System.out.println("Calling m2.start()");
   m2.start();

each object is now run in its own thread in parallel so the output is interleaved:

Calling m1.start()
Calling m2.start()
1 < From thread 1
1 < From thread 2
2 ...
2
3
3
4
4

The order could obviously vary depending on how the threads interleave.

One thing that may be confusing you is that you have chosen to extend Thread. This is discouraged. It is better to implement Runnable - like this:

public class Aaa implements Runnable {
  public void run() {
    for (int i = 1; i < 5; i++) {
      try {
        Thread.sleep(500); // sleeps thread
      } catch (InterruptedException e) {
        System.out.println(e);
      }
      System.out.println(i);
    }
  }

  public static void main(String[] args) {
    Aaa m1 = new Aaa(); // creating one object
    Thread t1 = new Thread(m1); // Its thread
    Aaa m2 = new Aaa(); // creating second object of a class
    Thread t2 = new Thread(m2); // Its thread
    t1.start(); // calls m's run method in a new thread.
    t2.start();
  }

}

Now it is clearer that your objects are being run in two different threads and therefore run in parallel.

Upvotes: 7

Md. Abdul Bari
Md. Abdul Bari

Reputation: 116

m1 and m2 gets the same priority from the main thread and working concurrently. sleep() allows the thread to go to block state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock.

Upvotes: 1

Maxim Efimov
Maxim Efimov

Reputation: 2737

Two threads are working simultaneously - it is the main point of thread usage. If you want something to be done not on main thread - you launch a new one. Or two if two tasks need to be executed and each of them works on it's own thread not waiting for another one.

Upvotes: 1

Related Questions