• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Sleep with and without instance Query

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sleep is a static method of thread class

let there be a Thread t



what does line 1 and line2 do....
Do line 1 causes the Thread t to sleep ?
Do line 2 also does the same thing ?

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling a static method on an instance of the class is same as calling it using the class name. So the call to t.sleep(2000) at line 2 is the same as Thread.sleep(2000)...
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code won't do anything since is doesn't compile because of 3 different reasons.
Then that is fixed it will cause the thread to sleep (execute nothing). Like Ankit said you can invoke static methods through an reference to an instance.
However this is not recommended because when you change the reference type (anyone got a better name for reference type?) you'll invoke a different static method.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling a static method on an instance of the class is same as calling it using the class name. So the call to t.sleep(2000) at line 2 is the same as Thread.sleep(2000)...


so t.sleep and Thread.sleep would cause the newly created thread to sleep
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile error because you didn't enclose the second sleep in a try catch.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohit, what does the javadocs for sleep method say?? I believe it says
Causes the currently executing thread to sleep

So it basically sleeps the thread which calls it. Period...
 
Gari Jain
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t.sleep() is same as Thread.sleep()-will put the thread in current execution to sleep.

don't get confused by code which seems to call sleep on some other thread instance.

--will put to sleep the current thread in execution only because anotherThread.sleep(); is same as Thread.sleep();
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread anotherThread at=new Thread(--runnable object--);

anotherThread.sleep();


If the following code is in main method
then,


would cause the main Thread to sleep and not the anotherThread
But if same code was in run method of anotherThread,then


would cause the anotherThread to sleep and not the main Thread

Am i right ?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic