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
Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.
sleep() interacts with the thread scheduler to put the current thread in wait state for specified period of time. Once the wait time is over, thread state is changed to runnable state and wait for the CPU for further execution.
Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t. sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.
Sleep() method belongs to Thread class. Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization. Wait() should be called only from Synchronized context.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With