• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling Sleep with and without instance Query

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)...
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile error because you didn't enclose the second sleep in a try catch.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic