In multithreading, why the other threads are not working when I have paused one thread for execution

Multi tool use
Multi tool use


In multithreading, why the other threads are not working when I have paused one thread for execution



I have three threads in my programming.According to the threading concept all the threads run asynchronously. So every thread is executed by the processor randomly. If I have just paused the execution of one thread by using thread.sleep() then other thread should work . Right??
And when the sleep time for the thread is over then it should again continue with its execution.
But in my case when I am pausing one thread by using t2.sleep(10000) then all other threads are also getting paused. But they should have worked normally while the t2 thread is in wait.Can anyone tell me why is it so?


import java.util.*;
class square extends Thread
{
public int x;
public square(int x)
{
this.x = x;
}
public void run()
{
System.out.println("Square of the number is " + x*x);
}
}
class cube extends Thread
{
public int x;
public cube(int x)
{
this.x = x;
}
public void run()
{
System.out.println("Cube of the number is " + x*x*x);
}
}

class randgen extends Thread
{
public void run()
{
int num=0;
Random r = new Random();
try
{
for(int i=0;i<5;i++)
{
num = r.nextInt(100);
System.out.println("The number generated is " + num);
square t2 = new square(num);
t2.start();
t2.sleep(10000);
cube t3 = new cube(num);
t3.start();

}
}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
public class Main
{
public static void main(String args)
{
randgen obj = new randgen();
obj.start();

}
}



OUTPUT



The number generated is 50
Square of the number is 2500
(After 10s)
Cube of the number is 125000
The number generated is 36
Square of the number is 1296
(After 10s)
The number generated is 75
Cube of the number is 46656
Square of the number is 5625
(After 10s)
The number generated is 92
Cube of the number is 421875
Square of the number is 8464
(After 10s)
The number generated is 0
Cube of the number is 778688
Square of the number is 0
(After 10s)
Cube of the number is 0



Why am I getting the output after 10s ,instead by the concept of thread that thread which I am pausing should only pause for 10s and other thread should work synchronously.





You sleep for 10 seconds before starting the cube thread. Why would it run before the 10 seconds are over?
– JB Nizet
Jul 1 at 13:02






You do not sleep t2, sleep(int millis) is a static method.
– Turing85
Jul 1 at 13:03



t2


sleep(int millis)





@JB Nizet yaa thats my point cube thread should not work before 10s but what I can see is that the other threads are not working in that 10s. Why?
– Himanshu Poddar
Jul 1 at 13:11





They are, since they print their result immediately. Read our output: the is printed immediately after youve started the square thread, and the cube only appears 10 seconds later, because you start the cube thread 10 seconds later.
– JB Nizet
Jul 1 at 13:23




3 Answers
3



Thread.sleep is a static method. Calling t2.sleep(10000) does not pause t2, it pauses the current thread. So what you are actually doing is starting t2, waiting 10 seconds, and then starting t3. If you want to pause thread t2, do not call t2.sleep(10000) from randgen, instead call Thread.sleep(10000) from inside square.run().


Thread.sleep


t2.sleep(10000)


t2.sleep(10000)


Thread.sleep(10000)


square.run()


public void run()
{
Thread.sleep(10000);
System.out.println("Square of the number is " + x*x);
}



because you are calling t2.sleep(10000); from the main thread which cause it to sleep (even though you access it with t2)


t2.sleep(10000);



You can see the answer also here



You can't force other thread to sleep by default.


square t2 = new square(num);
t2.start();
t2.sleep(10000);
cube t3 = new cube(num);
t3.start();



You are sleeping for 10 seconds before you start t3.


t3



Try to put sleep() inside the class:


sleep()


public void run()
{
System.out.println("Square of the number is " + x*x);
sleep(10000);
}





He thought that would make t2 sleep, not the current thread. Which do make sense by the syntax
– Roee Gavirel
Jul 1 at 13:03






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

9HbVkJLhpr,77YuP,lhBZ5
khUJNdea5SRNGr2e6 WLdvZY0n1cKbfL s2,9pjf05Hv8,U1ymceZZdt p

Popular posts from this blog

Rothschild family

Cinema of Italy